Android flatter-Navigator.pushNamed在调用路由时使应用程序崩溃(在FutureBuilder中)
因此,基本上我正在开发一个货币应用程序,在你用应用程序货币向你的帐户收费后,我验证了付款是否完成,我需要重定向到主页,但每次它都会崩溃,当所有其他自定义路径都在工作时 app.dart:(这是我的路线) 在confirmation.dart页面中,我通过调用运行正常的API来检查付款状态:Android flatter-Navigator.pushNamed在调用路由时使应用程序崩溃(在FutureBuilder中),android,ios,asynchronous,flutter,Android,Ios,Asynchronous,Flutter,因此,基本上我正在开发一个货币应用程序,在你用应用程序货币向你的帐户收费后,我验证了付款是否完成,我需要重定向到主页,但每次它都会崩溃,当所有其他自定义路径都在工作时 app.dart:(这是我的路线) 在confirmation.dart页面中,我通过调用运行正常的API来检查付款状态: import 'package:corsicoin/src/models/transaction.dart'; import 'package:flutter/material.dart'; import '
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;
}
}
import'package:corsicoin/src/models/transaction.dart';
进口“包装:颤振/材料.省道”;
导入“package:flatter/semantics.dart”;
进口“包装:颤振团/颤振团.飞镖”;
导入“../home_screen.dart”;
导入“../auth/auth.dart”;
导入“../lang/lang.dart”;
导入“../shared/current_credit.dart”;
类确认扩展了StatefulWidget{
最终字符串transactionToken;
@凌驾
确认书(此。transactionToken);
_ConfirmationState createState()=>U ConfirmationState();
}
类_ConfirmationState使用SingleTickerProviderStateMixin扩展状态{
String get transactionToken=>widget.transactionToken;
AuthBloc\u AuthBloc;
未来;;
@凌驾
void initState(){
打印('令牌:'+transactionToken);
_authBloc=BlocProvider.of(上下文);
_asyncFetch=getPaymentDetails(transactionToken);
super.initState();
}
@凌驾
无效处置(){
这就是处置;
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“确认”),//本地化文本(本地化.支付),
//前导:空,
//自动嵌入:false,
),
正文:buildPage()
);
}
小部件构建页面(){
回归未来建设者(
//\u initiatepayment()将等待Cyclos发送给bck的url
未来:_asynfetch,
生成器:(上下文,快照){
if(snapshot.hasData){
如果(snapshot.data==“true”){
//这将是支付成功后返回的内容
退回货柜(
填充物:LTRB(30,50,30150)的边缘设置,
子:列(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
中心(子项:LocalizedText(Localization.payment_done,textAlign:textAlign.Center,),
中心(子:图标)(
图标。勾选圆圈和轮廓,
颜色:颜色。绿色,
尺码:100,
)),
材料按钮(
最小宽度:500,
颜色:主题。背景。原色,
textColor:Colors.white,
填充:边缘设置。全部(12),
孩子:LocalizedText(Localization.ok.\u理解,),
已按下:(){
Navigator.pushNamed(上下文“/home”);
},
),
],
)
);
}否则{
//如果付款失败,我们将退还此款
退回货柜(
填充物:LTRB(30,50,30150)的边缘设置,
子:列(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
中心(子项:LocalizedText(Localization.payment_失败,textAlign:textAlign.Center,),
中心(子:图标)(
图标.block,
颜色:颜色,红色,
尺码:100,
)),
材料按钮(
最小宽度:500,
颜色:主题。背景。原色,
textColor:Colors.white,
填充:边缘设置。全部(12),
孩子:LocalizedText(Localization.ok.\u理解,),
已按下:(){
印刷品(“yo”);
},
),
],
)
);
}
}else if(snapshot.hasrerror){
返回脚手架(
正文:中(
子项:文本(“${snapshot.error}”),
),
);
}
返回脚手架(
正文:
纵队(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
容器(填料:边缘套全部(50),
子项:LocalizedText(Localization.wait_payment,textAlign:textAlign.center,),),
居中(子项:CircularProgressIndicator()),
]),
);
},
);
}
未来getPaymentDetails(字符串令牌)异步{
最终确认bool=wait_authBloc.authRepository.getPaymentDetails(令牌);
打印(“bool返回:+确认bool”);
返回确认布尔;
}
}
当我点击Raised按钮时,应用程序崩溃。
这是因为未来的构建器,还是因为/主页要求参数,但它甚至在进入确认页面之前就已经构建好了。以下是您需要的代码:
主屏幕。省道:
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,
);
}
}
导入“包装:颤振/材料.省道”;
导入“pay/pay_screen.dart”;
导入“cash/cash_screen.dart”;
导入“商店/商店屏幕.省道”;
导入“account/account_screen.dart”;
导入“account/shop_account_screen.dart”;
导入“转移/转移屏幕.省道”;
导入“钱包/钱包屏幕.省道”;
导入“../lang/lang.dart”;
类主屏幕扩展StatefulWidget{
最后一场比赛;
主屏幕({this.isShop});
_HomeScreenState createState()=>\u HomeScreenState();
}
类_homescrenstate扩展状态{
列出小部件;
int _currentIndex=2;
bool get isShop=>widget.isShop;
@凌驾
void initState(){
super.initState();
}
@凌驾
小部件构建(构建上下文){
_widgets=isShop
? [
ShopAccountScreen(),
ShopsScreen(),
CashScreenContainer(),
付费集装箱(
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
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