发现意外的 null 值

unexpected null value found

提问人:Allan 提问时间:10/19/2023 最后编辑:vkarpAllan 更新时间:10/23/2023 访问量:29

问:

我尝试运行我的 flutter 应用程序,运行后显示意外的空值,在调试控制台上显示问题出在 AppText.enText['welcome Text:']!,附件是 auth_page 类和 apptext 类的代码

import 'package:flutter/material.dart';
import 'package:laravel/components/login.dart';
import 'package:laravel/utils/config.dart';
import 'package:laravel/utils/text.dart';

class authPage extends StatefulWidget {
  const authPage({Key? key}) : super(key: key);
  @override
  State<authPage> createState() => _authPage();
}

class _authPage extends State<authPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
          child: SafeArea(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  AppText.enText['welcome Text:']!,
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 36,
                  ),
                ),
                Config.spaceSmall,

                Text(
                  AppText.enText['signIn Text:']!,
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 16,
                  ),
                ),
                Config.spaceSmall,

                //login components here
                loginForm(),
                Config.spaceSmall,
                Center(
                  child: TextButton(
                    onPressed: () {},
                    child: Text(
                      AppText.enText['Forgot Password']!,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                    ),
                  ),
                )

                //18
              ],
            ),
          )),
    );
  }
}

apptext 类:

class AppText {
  static final enText = {
    'welcome Text': 'Welcome',
    'signIn Text': 'signIn to your Account',
    'Register Text': 'Already have an Acount',
    'Registered Text':
        'You can easily Sign Up and connect to a doctor near you',
    'SignUp text': 'Dont have an account?',
    'Social login': 'or continue with social account',
    'Forgot Password': 'Forgot your Password',
  };
}

我尝试运行该应用程序,但出现红屏显示意外的空值

flutter null

评论


答:

0赞 Zhentao 10/19/2023 #1

显然,不包含像 和 这样的键,用 和 替换为 。AppText.enTextwelcome Text:signIn Text:welcome Text:welcome TextsignIn Text:signIn Text

0赞 Keval 10/19/2023 #2

您可以尝试以下代码进行调试:

Text(
    AppText.enText['welcome Text:'] ?? " Your Default Text",
    style: const TextStyle(
        fontWeight: FontWeight.bold,
        fontSize: 36,
    ),
),