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
相关产品推荐
相关产品推荐

