我是flutter的初学者,现在正在尝试制作应用程序。
我有3页。
第一页有一个带有Navigator. of(context).pushname('/第二')的提升按钮;第二页有一个text EditingController,text EditingController的值是[int]Evalue。我通过Navigator.of(context).pushReplace ement命名('/第三个参数:Evalue)更改屏幕。最后一页有一个提升按钮来显示Evalue,并通过Navigator.of(context).pop更改屏幕(Evalue)(我也从ModalRoute.of(context)的第二页获得了Evalue!.settings.参数)
但是当我来到第一页时,我没有从最后一页得到Evalue。我得到了空值。
这是我在第一页的代码。onP了:()async{
最终值=await Navigator. of(context).pushN的('/秒');
每次我都为空…
什么是问题伙计?从第三页到第一页有数据吗?
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'database.dart';
import 'alphabeta.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Future<Database> database = initDatabase();
return MaterialApp(
title: 'NoiceAnalytics',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {'/': (context) => WidgetApp(database),
'/first': (context) => first(database),
'/second': (context) => second(database),
'/third': (context) => third(database),
'
}
);
}
class WidgetApp extends StatefulWidget {
final Future<Database> db;
WidgetApp(this.db);
@override
_WidgetApp createState() => _WidgetApp();
}
class _WidgetApp extends State<WidgetApp> {
@override
Widget build(BuildContext context) {
database? db4;
return Scaffold(
body: Container(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: ElevatedButton(
child: Text(
'Noise', style: TextStyle(fontSize: 20),),
onPressed: () async {
final db3 = await Navigator.of(context).pushNamed('/second');
},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20))),
)
)
),
}
class second extends StatefulWidget {
final Future<Database> db;
second(this.db);
@override
State<StatefulWidget> createState() => _second();
}
class _second extends State<Backgroundnoise>{
TextEditingController? bgnController;
TextEditingController? measuretimeController;
@override
void initState(){
super.initState();
bgnController = new TextEditingController();
measuretimeController = new TextEditingController();
}
@override
Widget build(BuildContext context){
return Scaffold(
body: Container(
child: Center(
child: SingleChildScrollView(scrollDirection: Axis.vertical,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 100 ,bottom: 15),
child: Text('Background', style: TextStyle(fontSize: 20)),
),
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: TextField(
controller: bgnController,
decoration: InputDecoration(labelText: 'enter the value', hintText: 'Ex)80.5'),
keyboardType: TextInputType.number,
)
)
),
Padding(
padding: EdgeInsets.all(15),
child: Text('Time', style: TextStyle(fontSize: 20)),
),
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: TextField(
controller: measuretimeController,
decoration: InputDecoration(labelText: 'enter the value', hintText: 'Ex)80.5'),
keyboardType: TextInputType.number,
)
)
),
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: ElevatedButton(
child: Text('result', style: TextStyle(fontSize: 20),), onPressed: (){
database db1 = database(
bgn: bgnController!.value.text,
measuretime: measuretimeController!.value.text,
Navigator.of(context).pushReplacementNamed('/third', arguments: db1);
},
style: ElevatedButton.styleFrom(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(50))),
),
),
)
],
mainAxisAlignment: MainAxisAlignment.center,
),
),
),
),
);
}
}
class third extends StatefulWidget {
final Future<Database> db;
third(this.db);
@override
State<StatefulWidget> createState() => _third();
}
class _third extends State<Calculation> {
@override
Widget build(BuildContext context) {
final database db2 = ModalRoute.of(context)!.settings.arguments as database;
return Scaffold(
body: Container(
child: Center(
child: Column(children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15 ,bottom: 15),
child: Text('S.sound', style: TextStyle(fontSize: 20)),
),
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: ElevatedButton(
child: Text(db2.bgn.toString(), style: TextStyle(fontSize: 20),),
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50))),
),
),
),
Padding(
padding: EdgeInsets.all(15),
child: Text('Level', style: TextStyle(fontSize: 20)),
),
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: ElevatedButton(
child: Text(db2.measuretime.toString(), style: TextStyle(fontSize: 20),),
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50))),
),
),
),
Padding(
padding: EdgeInsets.only(top: 30),
child:
Row(children: <Widget>[
Padding(
padding: EdgeInsets.all(5),
child:
SizedBox(width: 200, height: 80,
child: ElevatedButton(
child: Text('Save and Quit', style: TextStyle(fontSize: 20),),
onPressed: (){
Navigator.of(context).pop(db2);
},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50)))
)
)
),
],mainAxisAlignment: MainAxisAlignment.center
)
)
],
mainAxisAlignment: MainAxisAlignment.center,
),
)
)
);
}
}
以及Database. dart在哪里
class database{
int? id;
String? bgn;
String? measuretime;
database({this.id, this.bgn ,this.measuretime});
Map<String, dynamic> toMap(){
return{
'id': id, 'bgn': bgn, 'measuretime': measuretime
};
}
}
我认为你的错误在于你如何从你推送的新屏幕中检索你的价值。
目前,据我所知,您正在使用:
final value = await Navigator.of(context).pushNamed('/second');
这确实会给你一个null
值。
您需要在新屏幕中使用settings.参数
。ModalRoute. of(context)!.settings.参数
您可以在此处找到更多信息:
https://docs.flutter.dev/cookbook/navigation/passing-datahttps://docs.flutter.dev/cookbook/navigation/navigate-with-arguments