Flutter 在应用程序的设置中单击配置文件时出错 导入“package:cached_network_image/cached_network_image.dart”; 进口“包装:颤振/材料.省道”; 导入“../models/user.dart”; 类配置文件AvatarWidget扩展了无状态小部件{ 最终用户; 虚拟世界({ 关键点, 这个用户, }):super(key:key); @凌驾 小部件构建(构建上下文){ 返回容器( 填充:边缘组。对称(垂直:30), 装饰:盒子装饰( 颜色:主题。背景。强调颜色, borderRadius:borderRadius.only(左下角:半径.圆形(30),右下角:半径.圆形(30)), ), 子:列( 儿童:[ 容器( 身高:160, 孩子:排( crossAxisAlignment:crossAxisAlignment.center, mainAxisAlignment:mainAxisAlignment.space, 儿童:[ //大小盒子( //宽度:50, //身高:50, //子项:材质按钮(标高:0, //填充:边缘集。全部(0), //按下:(){}, //子:图标(Icons.add,color:Theme.of(context.primaryColor), //颜色:主题。背景。强调颜色, //形状:StadiumBorder(), // ), // ), ClipRRect( borderRadius:borderRadius.all(半径圆形(300)), 子:CachedNetworkImage( 身高:135, 宽度:135, 适合:BoxFit.cover, imageUrl:user.image?.url, 占位符:(上下文,url)=>Image.asset( '资产/img/loading.gif', 适合:BoxFit.cover, 身高:135, 宽度:135, ), errorWidget:(上下文、url、错误)=>图标(Icons.error\u outline), ), ), //大小盒子( //宽度:50, //身高:50, //子项:材质按钮(标高:0, //填充:边缘集。全部(0), //按下:(){}, //子:图标(Icons.chat,颜色:Theme.of(context.primaryColor)), //颜色:主题。背景。强调颜色, //形状:StadiumBorder(), // ), // ), ], ), ), 正文( user.name, 样式:Theme.of(context).textTheme.headline5.merge(TextStyle(color:Theme.of(context.primaryColor)), ), 正文( user.address, 样式:Theme.of(context).textTheme.caption.merge(TextStyle(color:Theme.of(context.primaryColor)), ), ], ), ); } }
=======小部件库捕获的异常======================================================= 生成SettingsWidget时抛出了以下断言(脏,依赖项:[[u InheritedTheme,[u LocalizationsScope-[GlobalKey#6d165]],状态:[u SettingsWidgetState#7b9f4]: 必须向文本小部件提供非空字符串。 'package:flatter/src/widgets/text.dart': 断言失败:第378行第10位:“数据!”空' 导致错误的相关小部件是: 设置开关 file:///C:/Users/dell/Desktop/Tatkal%20app%20files/flutter_application/lib/route_generator.dart:96:50 引发异常时,这是堆栈:Flutter 在应用程序的设置中单击配置文件时出错 导入“package:cached_network_image/cached_network_image.dart”; 进口“包装:颤振/材料.省道”; 导入“../models/user.dart”; 类配置文件AvatarWidget扩展了无状态小部件{ 最终用户; 虚拟世界({ 关键点, 这个用户, }):super(key:key); @凌驾 小部件构建(构建上下文){ 返回容器( 填充:边缘组。对称(垂直:30), 装饰:盒子装饰( 颜色:主题。背景。强调颜色, borderRadius:borderRadius.only(左下角:半径.圆形(30),右下角:半径.圆形(30)), ), 子:列( 儿童:[ 容器( 身高:160, 孩子:排( crossAxisAlignment:crossAxisAlignment.center, mainAxisAlignment:mainAxisAlignment.space, 儿童:[ //大小盒子( //宽度:50, //身高:50, //子项:材质按钮(标高:0, //填充:边缘集。全部(0), //按下:(){}, //子:图标(Icons.add,color:Theme.of(context.primaryColor), //颜色:主题。背景。强调颜色, //形状:StadiumBorder(), // ), // ), ClipRRect( borderRadius:borderRadius.all(半径圆形(300)), 子:CachedNetworkImage( 身高:135, 宽度:135, 适合:BoxFit.cover, imageUrl:user.image?.url, 占位符:(上下文,url)=>Image.asset( '资产/img/loading.gif', 适合:BoxFit.cover, 身高:135, 宽度:135, ), errorWidget:(上下文、url、错误)=>图标(Icons.error\u outline), ), ), //大小盒子( //宽度:50, //身高:50, //子项:材质按钮(标高:0, //填充:边缘集。全部(0), //按下:(){}, //子:图标(Icons.chat,颜色:Theme.of(context.primaryColor)), //颜色:主题。背景。强调颜色, //形状:StadiumBorder(), // ), // ), ], ), ), 正文( user.name, 样式:Theme.of(context).textTheme.headline5.merge(TextStyle(color:Theme.of(context.primaryColor)), ), 正文( user.address, 样式:Theme.of(context).textTheme.caption.merge(TextStyle(color:Theme.of(context.primaryColor)), ), ], ), ); } },flutter,Flutter,=======小部件库捕获的异常======================================================= 生成SettingsWidget时抛出了以下断言(脏,依赖项:[[u InheritedTheme,[u LocalizationsScope-[GlobalKey#6d165]],状态:[u SettingsWidgetState#7b9f4]: 必须向文本小部件提供非空字符串。 'package:flatter/src/widgets/text.da
…我相信现在发生的是,
user.name
为空或不是字符串。因此,当您尝试执行文本(user.name)
时,会抛出一个错误。要查看是否确实如此,请设置一个断点并进行调试,或者在build方法中添加print(user.name)
,如下所示:
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import '../models/user.dart';
class ProfileAvatarWidget extends StatelessWidget {
final User user;
ProfileAvatarWidget({
Key key,
this.user,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(vertical: 30),
decoration: BoxDecoration(
color: Theme.of(context).accentColor,
borderRadius: BorderRadius.only(bottomLeft: Radius.circular(30), bottomRight: Radius.circular(30)),
),
child: Column(
children: <Widget>[
Container(
height: 160,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
// SizedBox(
// width: 50,
// height: 50,
// child: MaterialButton(elevation:0,
// padding: EdgeInsets.all(0),
// onPressed: () {},
// child: Icon(Icons.add, color: Theme.of(context).primaryColor),
// color: Theme.of(context).accentColor,
// shape: StadiumBorder(),
// ),
// ),
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(300)),
child: CachedNetworkImage(
height: 135,
width: 135,
fit: BoxFit.cover,
imageUrl: user.image?.url,
placeholder: (context, url) => Image.asset(
'assets/img/loading.gif',
fit: BoxFit.cover,
height: 135,
width: 135,
),
errorWidget: (context, url, error) => Icon(Icons.error_outline),
),
),
// SizedBox(
// width: 50,
// height: 50,
// child: MaterialButton(elevation:0,
// padding: EdgeInsets.all(0),
// onPressed: () {},
// child: Icon(Icons.chat, color: Theme.of(context).primaryColor),
// color: Theme.of(context).accentColor,
// shape: StadiumBorder(),
// ),
// ),
],
),
),
Text(
user.name,
style: Theme.of(context).textTheme.headline5.merge(TextStyle(color: Theme.of(context).primaryColor)),
),
Text(
user.address,
style: Theme.of(context).textTheme.caption.merge(TextStyle(color: Theme.of(context).primaryColor)),
),
],
),
);
}
}
此外,您可能应该按照构造函数中的要求标记user
参数。这样,如果您忘记通过IDE,IDE将向您发出警告。??
操作员
如果您不确定您的文本
小部件获得的是要显示的值还是空值,您可以使用?
操作符在左侧对象为空的情况下提供回退值
@override
Widget build(BuildContext context) {
print(user.name);
return Container(
...
有些地方的数据是空的,请注意显示您的代码。您的错误在第57行的文件“profile.dart”中。您试图创建文本小部件,但其中的值为null。那么如何解决此错误?有什么建议吗?你能给我看一下课程的代码吗?
S
,about文本是从那里来的。错误是about
string在您调用它时为空。不,它没有任何区别,仍然是相同的错误。@VED只是重复检查,您在SettingsWidget
StatefulWidget
中进行了编辑,对吗?根据您的错误复制/粘贴,这就是错误所在的位置。您问题中的代码片段显示了ProfileAvatarWidget
,这是一个无状态widget
@ambino,它没有帮助。
Text(user?.name ?? 'Missing')