android - 在 null 上调用了方法“文本”
问题描述
我正在编写一个笔记应用程序,我希望将数据从“新笔记”屏幕发送到所有其他笔记都存在的主屏幕。在运行时,当我按下保存笔记按钮时,我得到一个错误。
The following NoSuchMethodError was thrown while handling a gesture:
The method 'Text' was called on null.
Receiver: null
Tried calling: Text("yog")
这是我使用的主页代码(main.dart)
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:notes/newnote.dart';
void main() {
runApp(MaterialApp(home: MyApp(), initialRoute: 'main.dart', routes: {
'/home': (context) => MyApp(),
'/newnote': (context) => NewNote(),
}));
}
class dat {
final String title;
final String text;
dat(this.title, this.text);
}
int x = 0;
final datas = List<dat>.generate(
x,
(x) => dat(
'Note $x',
'A description of what needs to be done for Todo $x',
),
);
class MyApp extends StatefulWidget {
final List<dat> dats;
//requiring the list of todos
MyApp({Key key, @required this.dats}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Note-It!",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.black,
),
body: Column( //
children: <Widget>[
Padding(padding: EdgeInsets.fromLTRB(19.0, 19.0, 19.0, 19.0),),
Expanded(
child: ListView.builder(
itemCount: datas.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(datas[x].text),
onTap: () {
Navigator.push( context,MaterialPageRoute( builder: (context) =>
DetailScreen(notedata: datas[index])));
});
}),
),
Align(
alignment: Alignment.bottomRight,
child: FloatingActionButton(
child: Icon(Icons.add),
backgroundColor: Colors.black,
onPressed: () {
Navigator.pushNamed(context, '/newnote');
}),
),
],
),
);
}
}
class DetailScreen extends StatelessWidget {
// Declare a field that holds the data.
dat notedata;
// In the constructor, require a data.
DetailScreen({Key key, @required this.notedata}) : super(key: key);
@override
Widget build(BuildContext context) {
notedata = ModalRoute.of(context).settings.arguments;
// Use the data to create the UI.
return Scaffold(
appBar: AppBar(
title: Text(notedata.title),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Text(notedata.text),
),
);
}
}
这是用户添加新注释的页面的代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:notes/main.dart';
void main() {
runApp(MaterialApp(home: NewNote()));
}
String title = '';
String text = '';
final fromController1 = TextEditingController();
final fromController2 = TextEditingController();
var instance;
class NewNote extends StatefulWidget {
@override
_NewNoteState createState() => _NewNoteState();
}
class _NewNoteState extends State<NewNote> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("New Note"),
backgroundColor: Colors.black,
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.fromLTRB(19.0, 19.0, 19.0, 19.0),
child: SingleChildScrollView(
child: Column(children: <Widget>[
TextField(
controller: fromController1,
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: "Title"),
style: TextStyle(fontSize: 28.0),
),
Padding(padding: EdgeInsets.fromLTRB(19.0, 19.0, 19.0, 0.0)),
TextField(
controller: fromController2,
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: "Text"),
style: TextStyle(fontSize: 20.0),
maxLines: null,
),
Padding(padding: EdgeInsets.fromLTRB(19.0, 19.0, 19.0, 0.0)),
Align(
alignment: Alignment.bottomCenter,
child: FloatingActionButton.extended(
label: Text("Save Note"),
icon: Icon(Icons.save),
backgroundColor: Colors.black,
onPressed: () {
x++;
Navigator.pushNamed(context, '/home',
arguments: {
'title' : instance.Text(fromController1.text),
'text' : instance.Text(fromController2.text),
}
);
},
),
),
]),
),
),
),
);
}
}
解决方案
您已经通过构造函数传递数据,所以您不需要
notedata = ModalRoute.of(context).settings.arguments;
请参考您的代码如下:
class DetailScreen extends StatelessWidget {
// Declare a field that holds the data.
dat notedata;
// In the constructor, require a data.
DetailScreen({Key key, @required this.notedata}) : super(key: key);
@override
Widget build(BuildContext context) {
// Use the data to create the UI.
return Scaffold(
appBar: AppBar(
title: Text(widget.notedata.title),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Text(widget.notedata.text),
),
);
}
如果没有,这应该可以,然后让我知道
推荐阅读
- java - 发送 FCM 通知时自定义/停止退避/重试选项。
- core-data - 如何正确处理 watchOS CoreData 后台保存?
- php - 如何避免在 foreach 循环创建的行中重复字符串?
- svn - NAS 上的颠覆
- java - Flink Kafka 指标:如何获取它们
- c# - 将 base64 绑定到图像
- google-cloud-platform - 当我使用 Spring Cloud 版本 Hoxton-SR5 时,我的服务没有从 GCP Secret Manager 获取用户 ID 和密码
- java - Groovy 中的 GroupBy
- python - 如何使用 python 在 revit API 中复制对象
- c# - 我不能在 c# 中使用变量(Windows 窗体应用程序)