flutter - 在应用程序的设置中单击配置文件时出错
问题描述
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(dirty, dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#6d165]], state: _SettingsWidgetState#7b9f4) 时抛出了以下断言:A必须将非空字符串提供给 Text 小部件。'package:flutter/src/widgets/text.dart':断言失败:第 378 行 pos 10:'data != null' 相关的导致错误的小部件是:SettingsWidget file:///C:/Users/dell/Desktop /Tatkal%20app%20files/flutter_application/lib/route_generator.dart:96:50 抛出异常时,这是堆栈:
- #2 新文本(包:flutter/src/widgets/text.dart:378:10)
- #3 _SettingsWidgetState.build (包:markets/src/pages/settings.dart:165:39)
- #4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
- #5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)
- #6 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4667:11)
...
解决方案
我相信正在发生的事情user.name
是 null 或者不是字符串。因此,当您尝试执行时Text(user.name)
,会引发错误。要查看是否确实如此,请设置断点并进行调试,或者只是添加print(user.name)
构建方法,如下所示:
@override
Widget build(BuildContext context) {
print(user.name);
return Container(
...
此外,您可能应该user
在构造函数中将参数标记为必需。这样,如果您忘记传递它,您的 IDE 会警告您。
推荐阅读
- haskell - 在 Haskell 中,是否可以限定导入模块的一部分?
- javascript - 如何查看添加到元素的事件处理程序?
- eclipse-che - 无法使用多机堆栈启动/测试 Eclipse Che 工作区
- sql-server - 链接服务器 SQL 事务范围不起作用
- java - 如何更改警报对话框的图标?
- php - preg_replace 一个不在引号内的字符串
- java - 将 BigDecimal 转换为双精度值
- python - 需要帮助理解这行代码
- python - 需要帮助弄清楚为什么 G 没有在我的代码中定义?
- amazon-web-services - DynamoDB 读写能力说明