首页 > 解决方案 > 在应用程序的设置中单击配置文件时出错

问题描述


    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 抛出异常时,这是堆栈:

  1. #2 新文本(包:flutter/src/widgets/text.dart:378:10)
  2. #3 _SettingsWidgetState.build (包:markets/src/pages/settings.dart:165:39)
  3. #4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
  4. #5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)
  5. #6 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4667:11)

...

标签: flutter

解决方案


我相信正在发生的事情user.name是 null 或者不是字符串。因此,当您尝试执行时Text(user.name),会引发错误。要查看是否确实如此,请设置断点并进行调试,或者只是添加print(user.name)构建方法,如下所示:

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

此外,您可能应该user在构造函数中将参数标记为必需。这样,如果您忘记传递它,您的 IDE 会警告您。


推荐阅读