颤音:读取文件第一次失败,但在热重载后起作用
我尝试读取存储数据并在屏幕上显示的文件。当我第一次加载应用程序时,我会得到显示的错误。热重新加载后,将显示我的信息,而不会出错。您知道为什么会发生这种情况,以及如何确保我的数据已经第一次加载?
也许这与打电话有关,但我不知道。 底部的代码引起了问题:
儿童:text(context.Select((FileController Controller)=> controller.text!)),
import 'package:flutter/material.dart';
import 'package:habit_changer/file-handling/file_controller.dart';
import 'package:habit_changer/main/AddHabitDialog.dart';
import 'package:provider/provider.dart';
import '../utils/Constants.dart';
import 'MainBody.dart';
import 'nav_bar.dart';
void main() {
runApp(MultiProvider(
providers: [ChangeNotifierProvider(create: (_) => FileController())],
child: MaterialApp(home: MyApp())
));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
context.read<FileController>().readText();
return Scaffold(
appBar: AppBar(
title: const Text('Habits'),
backgroundColor: Constants.appBarColor,
leading: GestureDetector(
onTap: () {
openNavBar();
},
child: Icon(
Icons.menu, // add custom icons also
),
),
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 20.0),
child: GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) =>
AddHabitDialog().buildPopupDialog(context),
);
},
child: Icon(Icons.add),
),
)
],
),
body: Container(
child: Text(context.select((FileController controller) => controller.text!)),
),
);
}
}
这是错误:
Null check operator used on a null value
The relevant error-causing widget was:
MyApp file:///C:/Projekte/Flutter%20Projects/habit_changer/lib/main/main.dart:13:30
When the exception was thrown, this was the stack:
#0 MyApp.build.<anonymous closure> (package:habit_changer/main/main.dart:52:82)
#1 SelectContext.select (package:provider/src/inherited_provider.dart:283:32)
#2 MyApp.build (package:habit_changer/main/main.dart:52:29)
#3 StatelessElement.build (package:flutter/src/widgets/framework.dart:4648:28)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
I try to read a file where i have stored data and to show it on my screen. When I load my App for the first time i get the error shown bellow. After a hot reload my information will be displayed without any error. Do you know why this happens and how I can ensure that my data will be loaded already at the first time?
Maybe it has something to do with calling the state but I don't know.
It is the code at the bottom which causes the problem:
child: Text(context.select((FileController controller) =>
controller.text!)),
import 'package:flutter/material.dart';
import 'package:habit_changer/file-handling/file_controller.dart';
import 'package:habit_changer/main/AddHabitDialog.dart';
import 'package:provider/provider.dart';
import '../utils/Constants.dart';
import 'MainBody.dart';
import 'nav_bar.dart';
void main() {
runApp(MultiProvider(
providers: [ChangeNotifierProvider(create: (_) => FileController())],
child: MaterialApp(home: MyApp())
));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
context.read<FileController>().readText();
return Scaffold(
appBar: AppBar(
title: const Text('Habits'),
backgroundColor: Constants.appBarColor,
leading: GestureDetector(
onTap: () {
openNavBar();
},
child: Icon(
Icons.menu, // add custom icons also
),
),
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 20.0),
child: GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) =>
AddHabitDialog().buildPopupDialog(context),
);
},
child: Icon(Icons.add),
),
)
],
),
body: Container(
child: Text(context.select((FileController controller) => controller.text!)),
),
);
}
}
Here's the error:
Null check operator used on a null value
The relevant error-causing widget was:
MyApp file:///C:/Projekte/Flutter%20Projects/habit_changer/lib/main/main.dart:13:30
When the exception was thrown, this was the stack:
#0 MyApp.build.<anonymous closure> (package:habit_changer/main/main.dart:52:82)
#1 SelectContext.select (package:provider/src/inherited_provider.dart:283:32)
#2 MyApp.build (package:habit_changer/main/main.dart:52:29)
#3 StatelessElement.build (package:flutter/src/widgets/framework.dart:4648:28)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您的变量最初为 null 但后来收到该值时,可能会发生这种情况。将运算符更改为空检查运算符:
这将解决您收到的空异常。
This can happen when your variable is null initially but then receives the value later on. Change the operator to a null check operator:
This will solve the null exception that you are receiving.