在应用程序中的设置中单击配置文件时出错



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)),
),
],
),
);
}
}

==========小部件库捕获到异常=======================================================生成SettingsWidget时抛出了以下断言(脏,依赖项:[_IheritedTheme,_LocalizationsScope-[GlobalKey#6d165]],状态:_SettingsWidgetState#7b9f4(:必须向Text小部件提供非null字符串。'package:flutter/src/widgets/text.dart':断言失败:第378行位置10:"data!="null'导致错误的相关小部件是:设置小工具file:///C:/Users/dell/Desktop/Tatkal%20app%20files/flutter_application/lib/route_generator.dart:96:50当抛出异常时,这是堆栈:

  1. #2新文本(包:flutter/src/widgets/Text.dart:378:10(
  2. #3 _设置WidgetState.build(软件包:markets/src/pages/settings.dart:165:39(
  3. #4 StatefulElement.build(包:flutter/src/widgets/framework。dart:4612:27(
  4. #5 ComponentElement.performalRebuild(软件包:flutter/src/widgets/framework.dart:4495:15(
  5. #6 StatefulElement.performRebuild(包:flutter/src/widgets/framework.dart:4667:11(

我相信发生的事情是user.name为null或不是String。因此,当您尝试执行Text(user.name)时,会抛出一个错误。要查看情况是否确实如此,请设置断点并调试,或者在构建方法中添加print(user.name),如下所示:

@override
Widget build(BuildContext context) {
print(user.name);
return Container(
...

此外,您可能应该在构造函数中根据需要标记user参数。这样你的IDE会警告你,如果你忘记通过它。

??运算符

如果您不确定Text小部件是要显示值还是为null,则可以使用??运算符来提供回退值,以防左侧的对象为null。

Text(user?.name ?? 'Missing')

最新更新