【问题标题】:setState() or markNeedsBuild() error when trying to pick image from gallery and take image with camera flutter尝试从图库中选择图像并使用相机抖动拍摄图像时出现 setState() 或 markNeedsBuild() 错误
【发布时间】:2020-10-26 20:52:30
【问题描述】:

我正在尝试从我的图库中拍摄图像或立即使用相机拍摄上传页面。我创建了一个对话框来显示这两个选项,但出现错误

在构建期间调用 setState() 或 markNeedsBuild()。

当我在我的模拟器上运行时。
任何帮助将不胜感激。谢谢

这是我的代码


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


class UploadPage extends StatefulWidget {
  @override
  _UploadPageState createState() => _UploadPageState();
}

class _UploadPageState extends State<UploadPage> {
 PickedFile image;
 final picker=ImagePicker();

  takeImage(mContext){
    return showDialog(
      context: mContext,
      builder: (context){
        return SimpleDialog(
          title: Text("New post"),
          children: [
            SimpleDialogOption(
              child: Text("Upload from from gallery"),
              onPressed: ()async{
                Navigator.pop(context);
                final file=await picker.getImage(
                  source: ImageSource.gallery,
                //  maxHeight: 600,
                //  maxWidth: 970,
                );
                setState(() {
                  this.image=file;
                });
              },
            ),
            SimpleDialogOption(
              child: Text("Capture with camera"),
              onPressed: ()async{
               Navigator.pop(context);
               final file=await picker.getImage(
                 source: ImageSource.camera,
                 maxHeight: 600,
                 maxWidth: 970,
               );
               setState(() {
                 this.image=file;
               });
              },
            ),
            SimpleDialogOption(
              child: Text("Cancel",style: TextStyle(color: Colors.red),),
              onPressed: (){
                Navigator.pop(context);
              },
            ),
          ],
        );
      }
    );
  }
  @override
  Widget build(BuildContext context) {
    return takeImage(context);

  }
}


【问题讨论】:

    标签: image flutter upload


    【解决方案1】:

    showDialog 在应用的当前内容上方显示一个对话框并返回未来。

    构建后可以调用showDialog

    class UploadPage extends StatefulWidget {
      @override
      _UploadPageState createState() => _UploadPageState();
    }
    
    class _UploadPageState extends State<UploadPage> {
      PickedFile image;
      final picker = ImagePicker();
    
      takeImage(mContext) {
        return showDialog(
            context: mContext,
            builder: (context) {
              return SimpleDialog(
                title: Text("New post"),
                children: [
                  SimpleDialogOption(
                    child: Text("Upload from from gallery"),
                    onPressed: () async {
                      Navigator.pop(context);
                      final file = await picker.getImage(
                        source: ImageSource.gallery,
                        //  maxHeight: 600,
                        //  maxWidth: 970,
                      );
                      setState(() {
                        this.image = file;
                      });
                    },
                  ),
                  SimpleDialogOption(
                    child: Text("Capture with camera"),
                    onPressed: () async {
                      Navigator.pop(context);
                      final file = await picker.getImage(
                        source: ImageSource.camera,
                        maxHeight: 600,
                        maxWidth: 970,
                      );
                      setState(() {
                        this.image = file;
                      });
                    },
                  ),
                  SimpleDialogOption(
                    child: Text(
                      "Cancel",
                      style: TextStyle(color: Colors.red),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ],
              );
            });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              'Take Picture',
            ),
          ),
          body: Center(
            child: FlatButton(
              color: Colors.blue,
              onPressed: () {
                takeImage(context);
              },
              child: Text(
                'Select Image',
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多