You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter运行报错Null check operator used on a null value新手求助

问题原因
  • 代码中你自定义的MyApp组件没有被实际使用,main函数的runApp方法传入了未配置home属性的空MaterialApp实例。Flutter框架内部对MaterialApp的首页路由参数有非空断言校验,空值传入就会触发Null check operator used on a null value报错。
  • 存在冗余导入dart:io,该导入未在代码中使用,可直接删除。
修复方案

修改main方法的入参,将空的MaterialApp替换为你实现的MyApp实例即可,可选删除无用的导入语句。

修复后的完整代码如下:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget{
  // 构造方法增加const修饰属于Flutter性能优化写法,不影响基础功能
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.teal,
        body: SafeArea(
          child: Column (
            children: const <Widget>[
              CircleAvatar(
                radius: 50.0,
                backgroundColor: Colors.white,
              )
            ],
          ),
        ),
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者zxcvbnm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 18:45:03