Flutter,NoSuchMethod:getter 'notes' 被调用为 null
Flutter, NoSuchMethod: The getter 'notes' was called on null
我正在学习 flutter 并尝试制作一个笔记应用程序并使用继承的小部件和地图列表对 3 个笔记进行硬编码。问题是,到目前为止,当我尝试 运行 应用程序时会抛出此错误:
I/flutter (32083): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (32083): The following NoSuchMethodError was thrown building HomeScreen(dirty, state:
I/flutter (32083): _HomeScreenState#050b1):
I/flutter (32083): The getter 'notes' was called on null.
I/flutter (32083): Receiver: null
I/flutter (32083): Tried calling: notes
I/flutter (32083):
这是目前的代码:
main.dart:
import 'package:flutter/material.dart';
import 'home_screen.dart';
import 'note_inherited_widget.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return NoteInheritedWidget(
MaterialApp(
home: HomeScreen(),
theme: ThemeData(
primaryColor: Colors.deepOrange, accentColor: Colors.deepPurple),
),
);
}
}
home_screen.dart
import 'package:flutter/material.dart';
import 'package:simple_notes/note_inherited_widget.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
List<Map<String, String>> get notes => NoteInheritedWidget.of(context).notes;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Simple Notes'),
centerTitle: true,
),
body: ListView.builder(itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
NoteTitle(notes[index]['title']),
NoteText(notes[index]['text'])],
),
);
},
itemCount: notes.length,
),
bottomNavigationBar: (FlatButton(
onPressed: () {
showMyDialog(context);
},
child: Text('Add note'))));
}
Future<void> showMyDialog(
BuildContext context,
) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: TextField(),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.pop(context), child: Text('Save'))
],
);
});
}
}
class NoteTitle extends StatelessWidget {
final String title;
NoteTitle(this.title);
@override
Widget build(BuildContext context) {
return Text(
title,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold
),
);
}
}
class NoteText extends StatelessWidget {
final String text;
NoteText(this.text);
@override
Widget build(BuildContext context) {
return Text(
text,
style: TextStyle(
color: Colors.grey.shade600,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
);
}
}
note_inherited_widget.dart
import 'package:flutter/material.dart';
class NoteInheritedWidget extends InheritedWidget {
final notes = [
{
'title': 'My first title',
'text': 'My first text'
},
{
'title': 'My second title',
'text': 'My second text'
},
{
'title': 'My third title',
'text': 'My third text'
}
];
NoteInheritedWidget(Widget child) : super(child: child);
bool updateShouldNotify(NoteInheritedWidget oldwidget) => oldwidget.notes != notes;
static NoteInheritedWidget of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType(aspect: NoteInheritedWidget) as NoteInheritedWidget;
}
我在尝试实现继承的小部件方法时也遇到了问题,我认为那里可能存在我不理解的错误。因此,如果你们中的任何人可以帮助我解决这个问题,那就太好了。 @{11488366}
提前致谢!
你能试试这个吗;
List<Map<String, String>> notes;
@override
Widget build(BuildContext context) {
notes = NoteInheritedWidget.of(context).notes;
而不是;
List<Map<String, String>> get notes => NoteInheritedWidget.of(context).notes;
我想你需要做一个分离,像这样制作一个虚拟数据class:
class DummyDataProvider{
final notes = [
{
'title': 'My first title',
'text': 'My first text'
},
{
'title': 'My second title',
'text': 'My second text'
},
{
'title': 'My third title',
'text': 'My third text'
}
];
}
并在扩展 InheritedWidget 的小部件中创建此 class 的实例,以便您可以使用 InheritedWidget 提供相同的实例。
class NoteInheritedWidget extends InheritedWidget {
final dummyData = DummyDataProvider();
NoteInheritedWidget(Widget child) : super(child: child);
@override
bool updateShouldNotify(NoteInheritedWidget old) => dummyData != old.dummyData ;
static DummyDataProvider of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<NoteInheritedWidget>().dummyData;
}
并且不要忘记删除这行代码:
List<Map<String, String>> get notes => NoteInheritedWidget.of(context).notes;
并用此替换它并在 didChangeDependinces 方法中调用 of
方法,因为上下文已准备好在此方法中使用:
DummyDataProvider notes;
@override
void didChangeDependencies()
{
super.didChangeDependencies();
notes = NoteInheritedWidget.of(context);
}
或者在build
方法中调用
随时查看来自 here
的 dependOnInheritedWidgetOfExactType 文档
我正在学习 flutter 并尝试制作一个笔记应用程序并使用继承的小部件和地图列表对 3 个笔记进行硬编码。问题是,到目前为止,当我尝试 运行 应用程序时会抛出此错误:
I/flutter (32083): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (32083): The following NoSuchMethodError was thrown building HomeScreen(dirty, state:
I/flutter (32083): _HomeScreenState#050b1):
I/flutter (32083): The getter 'notes' was called on null.
I/flutter (32083): Receiver: null
I/flutter (32083): Tried calling: notes
I/flutter (32083):
这是目前的代码:
main.dart:
import 'package:flutter/material.dart';
import 'home_screen.dart';
import 'note_inherited_widget.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return NoteInheritedWidget(
MaterialApp(
home: HomeScreen(),
theme: ThemeData(
primaryColor: Colors.deepOrange, accentColor: Colors.deepPurple),
),
);
}
}
home_screen.dart
import 'package:flutter/material.dart';
import 'package:simple_notes/note_inherited_widget.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
List<Map<String, String>> get notes => NoteInheritedWidget.of(context).notes;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Simple Notes'),
centerTitle: true,
),
body: ListView.builder(itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
NoteTitle(notes[index]['title']),
NoteText(notes[index]['text'])],
),
);
},
itemCount: notes.length,
),
bottomNavigationBar: (FlatButton(
onPressed: () {
showMyDialog(context);
},
child: Text('Add note'))));
}
Future<void> showMyDialog(
BuildContext context,
) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: TextField(),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.pop(context), child: Text('Save'))
],
);
});
}
}
class NoteTitle extends StatelessWidget {
final String title;
NoteTitle(this.title);
@override
Widget build(BuildContext context) {
return Text(
title,
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold
),
);
}
}
class NoteText extends StatelessWidget {
final String text;
NoteText(this.text);
@override
Widget build(BuildContext context) {
return Text(
text,
style: TextStyle(
color: Colors.grey.shade600,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
);
}
}
note_inherited_widget.dart
import 'package:flutter/material.dart';
class NoteInheritedWidget extends InheritedWidget {
final notes = [
{
'title': 'My first title',
'text': 'My first text'
},
{
'title': 'My second title',
'text': 'My second text'
},
{
'title': 'My third title',
'text': 'My third text'
}
];
NoteInheritedWidget(Widget child) : super(child: child);
bool updateShouldNotify(NoteInheritedWidget oldwidget) => oldwidget.notes != notes;
static NoteInheritedWidget of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType(aspect: NoteInheritedWidget) as NoteInheritedWidget;
}
我在尝试实现继承的小部件方法时也遇到了问题,我认为那里可能存在我不理解的错误。因此,如果你们中的任何人可以帮助我解决这个问题,那就太好了。 @{11488366}
提前致谢!
你能试试这个吗;
List<Map<String, String>> notes;
@override
Widget build(BuildContext context) {
notes = NoteInheritedWidget.of(context).notes;
而不是;
List<Map<String, String>> get notes => NoteInheritedWidget.of(context).notes;
我想你需要做一个分离,像这样制作一个虚拟数据class:
class DummyDataProvider{
final notes = [
{
'title': 'My first title',
'text': 'My first text'
},
{
'title': 'My second title',
'text': 'My second text'
},
{
'title': 'My third title',
'text': 'My third text'
}
];
}
并在扩展 InheritedWidget 的小部件中创建此 class 的实例,以便您可以使用 InheritedWidget 提供相同的实例。
class NoteInheritedWidget extends InheritedWidget {
final dummyData = DummyDataProvider();
NoteInheritedWidget(Widget child) : super(child: child);
@override
bool updateShouldNotify(NoteInheritedWidget old) => dummyData != old.dummyData ;
static DummyDataProvider of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<NoteInheritedWidget>().dummyData;
}
并且不要忘记删除这行代码:
List<Map<String, String>> get notes => NoteInheritedWidget.of(context).notes;
并用此替换它并在 didChangeDependinces 方法中调用 of
方法,因为上下文已准备好在此方法中使用:
DummyDataProvider notes;
@override
void didChangeDependencies()
{
super.didChangeDependencies();
notes = NoteInheritedWidget.of(context);
}
或者在build
方法中调用
随时查看来自 here
的 dependOnInheritedWidgetOfExactType 文档