Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter NoSuchMethodError:方法';xxx和x27;在null(颤振小部件)上被调用_Flutter_Dart - Fatal编程技术网

Flutter NoSuchMethodError:方法';xxx和x27;在null(颤振小部件)上被调用

Flutter NoSuchMethodError:方法';xxx和x27;在null(颤振小部件)上被调用,flutter,dart,Flutter,Dart,我知道以前有人问过这个问题,但之前的问题及其答案并没有帮助我弄清楚为什么我总是遇到例外情况: NoSuchMethodError:对null调用了方法“showProgress”。 接收方:null尝试调用showProgress() 我创建了一个小部件类MyProgress import 'package:flutter/material.dart'; class MyProgress extends StatefulWidget { Color backgroundColor; C

我知道以前有人问过这个问题,但之前的问题及其答案并没有帮助我弄清楚为什么我总是遇到例外情况:

NoSuchMethodError:对null调用了方法“showProgress”。 接收方:null尝试调用showProgress()

我创建了一个小部件类MyProgress

import 'package:flutter/material.dart';

class MyProgress extends StatefulWidget {
  Color backgroundColor;
  Color color;
  Color containerColor;
  double borderRadius;
  String text;
  MyProgressState progressState;

  MyProgress(
    {
      this.backgroundColor = Colors.black54,
      this.color = Colors.white,
      this.containerColor = Colors.transparent,
      this.borderRadius = 10,
      this.text
    }
  );

  @override
  createState() => progressState = new MyProgressState(
      backgroundColor: this.backgroundColor,
      color: this.color,
      containerColor: this.containerColor,
      borderRadius: this.borderRadius,
      text: this.text);

  void hideProgress() {
    progressState.hideProgress();
  }

  void showProgress() {
    progressState.showProgress();
  }

  void showProgressWithText(String title) {
    progressState.showProgressWithText(title);
  }

  static Widget getMyProgress(String title) {
    return MyProgress(
      backgroundColor: Colors.black12,
      color: Colors.black,
      containerColor: Colors.white,
      borderRadius: 5,
      text: title,
    );
  }
}

class MyProgressState extends State<MyProgress> {
  Color backgroundColor;
  Color color;
  Color containerColor;
  double borderRadius;
  String text;
  bool _opacity = false;

  MyProgressState(    
    {
      this.backgroundColor = Colors.black54,
      this.color = Colors.white,
      this.containerColor = Colors.transparent,
      this.borderRadius = 10,
      this.text
    }
  );

  @override
  Widget build(BuildContext context) {
    return Container(
      child: !_opacity ? null : new Opacity(opacity: _opacity ? 1 : 0,
        child: Stack(
          children: <Widget>[
            new Center(
              child: Container(
                width: 300,
                height: 120,
                decoration: new BoxDecoration(
                  color: containerColor,
                  border: Border.all(color: Colors.orange),
                  boxShadow: [BoxShadow(blurRadius: 5)],
                  borderRadius: new BorderRadius.all(
                     new Radius.circular(borderRadius)
                  )
                ),
              ),
            ),
            new Center(
              child: _getCenterContent(),
            )
          ],
        ),
      )
    );
  }

  Widget _getCenterContent() {
    if (text == null || text.isEmpty) {
      return _getCircularProgress();
    }

    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _getCircularProgress(),
          new Container(
            margin: const EdgeInsets.fromLTRB(20, 0, 0, 0),
            child: Text(
              text,
              style: new TextStyle(color: color, fontSize: 18),
            ),
          )
        ],
      ),
    );
  }

  Widget _getCircularProgress() {
    return CircularProgressIndicator(
        valueColor: new AlwaysStoppedAnimation(Colors.deepOrange));
  }

  void hideProgress() {
    setState(() {
      _opacity = false;
    });
  }

  void showProgress() {
    setState(() {
      _opacity = true;
    });
  }

  void showProgressWithText(String title) {
    setState(() {
      _opacity = true;
      text = title;
    });
  }
}
导入“包装:颤振/材料.省道”;
类MyProgress扩展了StatefulWidget{
颜色背景色;
颜色;
彩色容器彩色;
双边界半径;
字符串文本;
MyProgressState progressState;
我的进展(
{
this.backgroundColor=Colors.black54,
this.color=Colors.white,
this.containerColor=Colors.transparent,
这个.borderRadius=10,
这个.text
}
);
@凌驾
createState()=>progressState=新的MyProgressState(
背景色:这个,
颜色:这个,
containerColor:this.containerColor,
borderRadius:this.borderRadius,
文本:这个文本);
void hideProgress(){
progressState.hideProgress();
}
void showProgress(){
progressState.showProgress();
}
void show progresswithtext(字符串标题){
progressState.showProgressWithText(标题);
}
静态小部件getMyProgress(字符串标题){
返回我的进度(
背景颜色:Colors.black12,
颜色:颜色,黑色,
容器颜色:颜色。白色,
边界半径:5,
正文:标题,
);
}
}
类MyProgressState扩展了状态{
颜色背景色;
颜色;
彩色容器彩色;
双边界半径;
字符串文本;
bool _不透明度=假;
MyProgressState(
{
this.backgroundColor=Colors.black54,
this.color=Colors.white,
this.containerColor=Colors.transparent,
这个.borderRadius=10,
这个.text
}
);
@凌驾
小部件构建(构建上下文){
返回容器(
子项:!\u不透明度?null:新不透明度(不透明度:\u不透明度?1:0,
子:堆栈(
儿童:[
新中心(
子:容器(
宽度:300,
身高:120,
装饰:新盒子装饰(
颜色:集装箱颜色,
边框:边框。全部(颜色:Colors.orange),
boxShadow:[boxShadow(模糊半径:5)],
borderRadius:新建borderRadius.all(
新半径。圆形(边界半径)
)
),
),
),
新中心(
子项:_getCenterContent(),
)
],
),
)
);
}
小部件_getCenterContent(){
if(text==null | | text.isEmpty){
返回_getCircularProgress();
}
返回中心(
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
_getCircularProgress(),
新容器(
边距:LTRB(20,0,0,0)的常数边集,
子:文本(
文本
样式:新文本样式(颜色:颜色,字体大小:18),
),
)
],
),
);
}
小部件_getCircularProgress(){
返回循环预测器(
valueColor:new AlwaysStoppedAnimation(Colors.deepOrange));
}
void hideProgress(){
设置状态(){
_不透明度=假;
});
}
void showProgress(){
设置状态(){
_不透明度=真;
});
}
void show progresswithtext(字符串标题){
设置状态(){
_不透明度=真;
文本=标题;
});
}
}
我在screen类中使用我的进度:ItemsLoad

class ItemsLoad extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return ItemsLoadState();
  }
}

class ItemsLoadState extends State<ItemsLoad> {
  var appBar = AppBar();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  List<Item> items;

  MyProgress progress = MyProgress.getMyProgress("Fetching Data ...");

  ItemsLoadState();

  @override
  Widget build(BuildContext context) {
    if (items == null) {
      items = List<Item>();
      progress.showProgress();
      requestData();
    }

    return Scaffold(
      appBar: AppBar(
        title: Text("Items Loading"),
      ),
      body: SingleChildScrollView(
          child: Stack(
            children: <Widget>[
              Container(
                height: (MediaQuery.of(context).size.height - (appBar.preferredSize.height * 2)),
                padding: const EdgeInsets.symmetric(horizontal: 5),
                child: ListView.builder(
                  physics: BouncingScrollPhysics(),
                      itemCount: items.length,
                      itemBuilder: bookListView,
                ),
              ),
              Container(
                height: (MediaQuery.of(context).size.height - (appBar.preferredSize.height * 2)),
                padding: const EdgeInsets.symmetric(horizontal: 10),
                child: progress,
              ),
            ],
          ),
        ),
    );
  }
}
class ItemsLoad扩展了StatefulWidget{
@凌驾
状态createState(){
返回ItemsLoadState();
}
}
类ItemsLoadState扩展状态{
var-appBar=appBar();
最终GlobalKey _scaffoldKey=GlobalKey();
清单项目;
MyProgress=MyProgress.getMyProgress(“获取数据…”);
ItemsLoadState();
@凌驾
小部件构建(构建上下文){
if(items==null){
项目=列表();
progress.showProgress();
请求数据();
}
返回脚手架(
appBar:appBar(
标题:文本(“项目加载”),
),
正文:SingleChildScrollView(
子:堆栈(
儿童:[
容器(
高度:(MediaQuery.of(context.size.height-(appBar.preferredSize.height*2)),
填充:常量边集。对称(水平:5),
子项:ListView.builder(
物理:弹跳CrollPhysics(),
itemCount:items.length,
itemBuilder:bookListView,
),
),
容器(
高度:(MediaQuery.of(context.size.height-(appBar.preferredSize.height*2)),
填充:常量边集。对称(水平:10),
儿童:进步,
),
],
),
),
);
}
}

我会很感激如何修复这个异常,因为它发生在屏幕首次加载时,但如果点击一个操作按钮,它会正常工作,没有任何异常。我仍在学习使用颤振,这里可能会遗漏一些概念。

问题是您正在小部件中存储状态。小部件应该是无状态的,因为在重建时可能会创建新实例。当使用小部件的新实例重新构建时,并且类型和键与同一位置的前一个小部件的类型和键相匹配,则重用元素和State子类的实例。在这种情况下,不会再次调用
createState


在您的情况下,当小部件由于某种原因重新生成时,
progressState
将为空。

传递给showProgress()函数的值似乎为空。请在调用showProgress()函数之前检查此值