如何修复构建函数返回 null。?

Posted

技术标签:

【中文标题】如何修复构建函数返回 null。?【英文标题】:How to Fix A build function returned null.? 【发布时间】:2021-04-02 19:31:44 【问题描述】:

我尝试在设备上运行

代码:

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';



// ignore: camel_case_types
class profileScreen extends StatefulWidget 
profileScreen(Key key) : super(key: key);

@override
_profileScreen createState() => _profileScreen();


// ignore: camel_case_types
class _profileScreen extends State<profileScreen> 

@override
Widget build(BuildContext context) 

return Scaffold(
    body: profileView()
);


Widget profileView() 
return Column(
  children: <Widget>[
    Padding(
      padding: EdgeInsets.fromLTRB(30, 50, 30, 30),
      child: Row(
        mainAxisAlignment: 
    MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(height: 50, width: 50 ,child: 
    Icon(Icons.arrow_back_ios, size: 24,color: 
   Colors.black54,), decoration: 
   BoxDecoration(border: Border.all(color: 
   Colors.black54), borderRadius: 
   BorderRadius.all(Radius.circular(10))),),
          Text('Profiles details', style: 
   TextStyle(fontSize: 18, fontWeight: 
  FontWeight.bold),),
          Container(height: 24,width: 24)
        ],
      ),
    ),
    Padding(
      padding: const EdgeInsets.fromLTRB(0, 0,0 ,50),
      child: Stack(
        children: <Widget>[
          CircleAvatar(
            radius: 70,
            child: ClipOval(child: Image.asset('', height: 150, width: 150, fit: BoxFit.cover,),),
          ),
          Positioned(bottom: 1, right: 1 ,child: Container(
            height: 40, width: 40,
            child: Icon(Icons.add_a_photo, color: Colors.deepOrange,),
            decoration: BoxDecoration(
                color: Colors.deepOrange,
                borderRadius: BorderRadius.all(Radius.circular(20))
            ),
          ))
        ],
      ),
    ),
    Expanded(child: Container(
      decoration: BoxDecoration(
          borderRadius: BorderRadius.only(topLeft: Radius.circular(30), topRight: Radius.circular(30)),
          gradient: LinearGradient(
              begin: Alignment.topRight,
              end: Alignment.bottomLeft,
              colors: [Colors.black54, Color.fromRGBO(0, 41, 102, 1)]
          )
      ),
      child: Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.fromLTRB(20, 25, 20, 4),
            child: Container(
              height: 60,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('Name', style: TextStyle(color: Colors.white70),),
                ),
              ), decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(20)),border: Border.all(width: 1.0, color: Colors.white70)),
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(20, 5, 20, 4),
            child: Container(
              height: 60,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('Email', style: TextStyle(color: Colors.white70),),
                ),
              ), decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(20)),border: Border.all(width: 1.0, color: Colors.white70)),
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(20, 5, 20, 4),
            child: Container(
              height: 60,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('Type something about yourself', style: TextStyle(color: Colors.white70),),
                ),
              ), decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(20)),border: Border.all(width: 1.0, color: Colors.white70)),
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(20, 5, 20, 4),
            child: Container(
              height: 60,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text('Phone number', style: TextStyle(color: Colors.white70),),
                ),
              ), decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(20)),border: Border.all(width: 1.0, color: Colors.white70)),
            ),
          ),
          Expanded(
            child: Align(
              alignment: Alignment.bottomRight,
              child: Container( height: 70, width: 200,
                child: Align(child: Text('Save', style: TextStyle(color: Colors.white70, fontSize: 20),),),
                decoration: BoxDecoration(
                    color: Colors.deepOrange,
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(30),)
                ),
              ),
            ),
          )
        ],
      ),
    ))
  ],
);
 
   

小部件库捕获的异常以下断言是 throw building Builder(dirty):构建函数返回 null。

有问题的小部件是:Builder 构建函数永远不能返回 空。

返回导致建筑小部件填充的空白空间 可用空间,返回“Container()”。返回一个空白空间 占用尽可能少的空间,返回 "Container(width: 0.0, 高度:0.0)"。

相关的导致错误的小部件是:MaterialApp file:///C:/Users/PC-Admin/Desktop/faceadmin/lib/main.dart:15:12 什么时候 抛出异常,这是堆栈: #0 调试小部件生成器值。 (包:flutter/src/widgets/debug.dart:302:7) #1 debugWidgetBuilderValue (package:flutter/src/widgets/debug.dart:323:4) #2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4632:7) #3 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5) #4 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5)

【问题讨论】:

可能还有错误,在控制台查看第一个错误 【参考方案1】:

sn-p 对我有用。我认为唯一的问题可能是格式/缩进或颤动。尝试运行flutter clean,然后运行项目。

【讨论】:

以上是关于如何修复构建函数返回 null。?的主要内容,如果未能解决你的问题,请参考以下文章

如何修复 System.Xml.Linq.XContainer.Element(...) 返回 null

Flutter Future:构建函数返回 null

FutureBuilder 构建函数不能返回 Null 错误

错误:如何在 Opencv python 中修复“SystemError:<built-in function imshow> 返回 NULL 而未设置错误”

如何从流生成器返回 null

如何修复返回“未定义”的异步函数?