Flutter - Navigator.pushNamed делает приложение cra sh при вызове маршрута (внутри FutureBuilder) - PullRequest
0 голосов
/ 06 января 2020

Таким образом, в основном я работаю над приложением валюты, и после того, как вы зачислили на свой счет валюту приложения, я проверил, если платеж выполнен, или нет, мне нужно перенаправить на домашнюю страницу, но он происходит сбой каждый раз, когда все другие пользовательские маршруты работают.

app.dart: (это мои маршруты)

navigatorKey: _navigatorKey,
        initialRoute: '/',
        routes: {
          '/register': (context) => RegisterScreen(),
          '/login': (context) => LoginScreen(),
          '/home': (context) => HomeScreen(),
          '/info': (context) => InfoPage(),
          '/accueil': (context) => Onboarding(),
          '/wallet' : (context) => WalletScreenContainer(),
        },

На странице подтверждения.дарт я проверяю состояние платежа, вызывая API, который отлично работает:

import 'package:corsicoin/src/models/transaction.dart';
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../home_screen.dart';

import '../../auth/auth.dart';
import '../../lang/lang.dart';
import '../shared/current_credit.dart';

class Confirmation extends StatefulWidget {
  final String transactionToken;
  @override
  Confirmation(this.transactionToken);

  _ConfirmationState createState() => _ConfirmationState();
}

class _ConfirmationState extends State<Confirmation> with SingleTickerProviderStateMixin {
  String get transactionToken => widget.transactionToken;
  AuthBloc _authBloc;
  Future<String> _asyncFetch;


  @override
  void initState() {
    print('token : '+transactionToken);
    _authBloc = BlocProvider.of<AuthBloc>(context);
    _asyncFetch = getPaymentDetails(transactionToken);
    super.initState();
  }

  @override
    void dispose(){
    this.dispose;
      super.dispose();
    }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Confirmation"),//LocalizedText(Localization.payment),
        // leading: null,
        // automaticallyImplyLeading: false,
      ),
      body: buildPage()
    );
  }

  Widget buildPage() {
    return FutureBuilder<String>(
      // _initiatepayment() will await for the url sended bck by Cyclos
      future: _asyncFetch,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          if(snapshot.data == "true"){
            //this will  be what is returned if the payment is successful
            return Container( 
              padding: EdgeInsets.fromLTRB(30,50,30,150),
              child : Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                Center(child : LocalizedText(Localization.payment_done, textAlign: TextAlign.center,)),
                Center(child :Icon(
                  Icons.check_circle_outline,
                  color: Colors.green,
                  size : 100,
                )),
                MaterialButton(
                  minWidth: 500,
                  color: Theme.of(context).primaryColor,
                  textColor: Colors.white,
                  padding: EdgeInsets.all(12),
                  child: LocalizedText(Localization.ok_understood,),
                  onPressed: (){
                    Navigator.pushNamed(context, '/home');
                  },
                ),
              ],
              )
            );
          } else {
            //this is waht we return if the payment failed
            return Container( 
              padding: EdgeInsets.fromLTRB(30,50,30,150),
              child : Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                Center(child : LocalizedText(Localization.payment_failed, textAlign: TextAlign.center,)),
                Center(child :Icon(
                  Icons.block,
                  color: Colors.red,
                  size : 100,
                )),
                MaterialButton(
                  minWidth: 500,
                  color: Theme.of(context).primaryColor,
                  textColor: Colors.white,
                  padding: EdgeInsets.all(12),
                  child: LocalizedText(Localization.ok_understood,),
                  onPressed: (){
                    print("yo");
                  },
                ),
              ],
              )
            );
          }
        } else if (snapshot.hasError) {
          return Scaffold(
            body: Center(
              child: Text("${snapshot.error}"),
            ),
          );
        }
        return Scaffold(
          body: 
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Container(padding: EdgeInsets.all(50),
                child : LocalizedText(Localization.wait_payment, textAlign: TextAlign.center,),),
              Center(child : CircularProgressIndicator()),
          ]),
        );
      },
    );
  }

  Future<String> getPaymentDetails(String token) async {
    final confirmationBool = await _authBloc.authRepository.getPaymentDetails(token);
    print("bool returned : "+confirmationBool);
    return confirmationBool;
  }
}

И когда я нажимаю на RaisedButton, приложение вылетает. Так из-за Future Builder или из-за того, что / home page запрашивает параметры, но он уже был собран еще до того, как попасть на страницу подтверждения. вот код, если вы хотите:

home_screen.dart:

import 'package:flutter/material.dart';

import 'pay/pay_screen.dart';
import 'cash/cash_screen.dart';
import 'shops/shops_screen.dart';
import 'account/account_screen.dart';
import 'account/shop_account_screen.dart';
import 'transfer/transfer_screen.dart';
import 'wallet/wallet_screen.dart';
import '../lang/lang.dart';

class HomeScreen extends StatefulWidget {
  final bool isShop;
  HomeScreen({this.isShop});
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  List<Widget> _widgets;
  int _currentIndex = 2;

  bool get isShop => widget.isShop;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _widgets = isShop
        ? [
            ShopAccountScreen(),
            ShopsScreen(),
            CashScreenContainer(),
            PayScreenContainer(),
            TransferScreen(),
          ]
        : [
            WalletScreenContainer(),
            ShopsScreen(),
            PayScreenContainer(),
            TransferScreen(),
            AccountScreen(),
          ];

    return Scaffold(
      body: _widgets.elementAt(_currentIndex),
      bottomNavigationBar: NavigationBar(
        currentIndex: _currentIndex,
        isShop: isShop,
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      resizeToAvoidBottomInset: _currentIndex != 2 || _currentIndex != 3,
    );
  }
}

class NavigationBar extends StatelessWidget {
  final int currentIndex;
  final void Function(int index) onTap;
  final bool isShop;
  NavigationBar({this.currentIndex, this.onTap, this.isShop});

  @override
  Widget build(BuildContext context) {
    return isShop
        ? _buildNavigationBarForShop(context)
        : _buildNavigationBarForUser(context);
  }

  Widget _buildNavigationBarForUser(BuildContext context) {
    return BottomNavigationBar(
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.account_balance_wallet),
          title: LocalizedText(Localization.menu_wallet),
          backgroundColor: Colors.white,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.shopping_cart),
          title: LocalizedText(Localization.menu_shops),
          backgroundColor: Colors.white,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.credit_card),
          title: LocalizedText(Localization.menu_pay),
          backgroundColor: Colors.white,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.swap_horiz),
          title: LocalizedText(Localization.menu_transfer),
          backgroundColor: Colors.white,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.person),
          title: LocalizedText(Localization.menu_account),
          backgroundColor: Colors.white,
        ),
      ],
      currentIndex: currentIndex,
      type: BottomNavigationBarType.fixed,
      // backgroundColor: Colors.white,
      // selectedItemColor: Theme.of(context).primaryColor,
      // unselectedItemColor: Theme.of(context).colorScheme.secondary,
      onTap: onTap,
    );
  }

  Widget _buildNavigationBarForShop(BuildContext context) {
    return BottomNavigationBar(
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.business_center),
          title: LocalizedText(Localization.menu_account),
          backgroundColor: Colors.white,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.shopping_cart),
          title: LocalizedText(Localization.menu_shops),
          backgroundColor: Colors.white,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.arrow_downward),
          title: LocalizedText(Localization.menu_cash),
          backgroundColor: Colors.white,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.credit_card),
          title: LocalizedText(Localization.menu_pay),
          backgroundColor: Colors.white,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.swap_horiz),
          title: LocalizedText(Localization.menu_transfer),
          backgroundColor: Colors.white,
        ),
      ],
      currentIndex: currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: onTap,
    );
  }
}

name: corsicoin
description: Corsicoin app.

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 0.1.46

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

  shared_preferences: ^0.5.2
  cached_network_image: ^1.1.0
  flutter_svg: ^0.13.1
  rflutter_alert: ^1.0.3
  flutter_bloc: ^0.11.1
  equatable: ^0.1.6
  flutter_secure_storage: ^3.2.1
  local_auth: ^0.4.0+1
  http_auth: ^0.2.5
  firebase_core: ^0.4.0+6
  firebase_messaging: ^5.1.3
  cloud_firestore: ^0.12.9+6
  firebase_auth: ^0.14.0+5
  validate: ^1.7.0
  kiwi: ^0.1.0
  image_picker: ^0.6.1
  flutter_crashlytics: ^1.0.0
  flutter_sticky_header: ^0.4.0
  dio: ^2.1.0
  auto_size_text: ^1.1.1
  flutter_masked_text: ^0.8.0
  permission_handler: '^3.2.0'
  path_provider: ^0.5.0+1
  flutter_email_sender: ^2.0.0
  flutter_swiper: ^1.1.6
  contacts_service: ^0.2.8
  geolocator: ^5.1.1+1
  google_maps_flutter: ^0.5.20+1
  url_launcher: ^5.0.2
  qr_mobile_vision: ^0.2.2
  qr_flutter: ^2.0.0+51
  csv: ^4.0.3
  share_extend: ^1.0.8
  country_pickers: ^1.1.0
  diacritic: ^0.1.1
  recase: ^2.0.1
  keyboard_visibility: ^0.5.6

  http: 0.12.0
  flutter_webview_plugin: ^0.3.10
  webview_flutter : ^0.3.17

  flutter_worldpay:
    path: ../flutter_worldpay/

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  assets:
    - assets/images/
    - assets/icons/
    - assets/html/

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.io/custom-fonts/#from-packages
  fonts:
    - family: Lato
      fonts:
        - asset: assets/fonts/Lato.ttf
        - asset: assets/fonts/Lato-Light.ttf
          weight: 300
        - asset: assets/fonts/Lato-Bold.ttf
          weight: 700
    - family: Nunito
      fonts:
        - asset: assets/fonts/Nunito.ttf
        - asset: assets/fonts/Nunito-Light.ttf
          weight: 300
        - asset: assets/fonts/Nunito-Bold.ttf
          weight: 700

И все в порядке в флаттер врач

Ответы [ 2 ]

0 голосов
/ 07 января 2020

Нет, все в порядке, я нашел решение, передав параметры в моем:

Navigator.of(context).pop(true) //if what i wanted to happen is good

or 

Navigator.of(context).pop('error') //if what i wanted to happen went bad
0 голосов
/ 06 января 2020

Наиболее вероятная причина взлома sh, кажется, isShop логическое значение в HomeScreen.

Ниже приведены причины:

  • isShop необязательное значение bool, которое означает, что оно останется null, если явно не указано значение
  • 'isShop' используется без инициализации в initState для обработки сценария по умолчанию

Чтобы решить эту проблему, просто замените:

HomeScreen({this.isShop});

на

HomeScreen({this.isShop = false});

и дайте мне знать, если вам нужна дополнительная помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...