颤音:读取文件第一次失败,但在热重载后起作用

发布于 2025-01-18 14:46:11 字数 2843 浏览 0 评论 0原文

我尝试读取存储数据并在屏幕上显示的文件。当我第一次加载应用程序时,我会得到显示的错误。热重新加载后,将显示我的信息,而不会出错。您知道为什么会发生这种情况,以及如何确保我的数据已经第一次加载?

也许这与打电话有关,但我不知道。 底部的代码引起了问题:

儿童: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!)),

Error after first load
Everything works after hot reloading

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喵星人汪星人 2025-01-25 14:46:11

当您的变量最初为 null 但后来收到该值时,可能会发生这种情况。将运算符更改为空检查运算符:

controller.text ?? ''

这将解决您收到的空异常。

This can happen when your variable is null initially but then receives the value later on. Change the operator to a null check operator:

controller.text ?? ''

This will solve the null exception that you are receiving.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文