flutter - Flutter错误Column的子级不能包含任何空值,但是在索引0处找到了空值

标签 flutter dart

我在尝试使用照相机应用程序时遇到问题,即列的子代不能包含空值,但是在索引0处找到了空值。我无法在列的子代中传递小部件。

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

void main() {
  runApp(new MaterialApp(
    title: "Camera App",
    home: LandingScreen(),
    ));
}

class LandingScreen extends StatefulWidget {
  @override
  _LandingScreenState createState() => _LandingScreenState();
}

class _LandingScreenState extends State<LandingScreen> {

  File imageFile;

  _openGallery(BuildContext context) async {
    var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
    this.setState((){
      imageFile = picture;
    });
    Navigator.of(context).pop();
  }

  _openCamera(BuildContext context) async{
    var picture = await ImagePicker.pickImage(source: ImageSource.camera);
    this.setState((){
      imageFile = picture;
    });
    Navigator.of(context).pop();
  }

  Future<void> _showChoiceDialog(BuildContext context){
    return showDialog(context: context,builder: (BuildContext context){
      return AlertDialog(
        title: Text("Make a choice!"),
        content: SingleChildScrollView(
          child: ListBody(
            children: <Widget>[
              GestureDetector(
                child: Text("Gallery"),
                onTap: (){
                  _openGallery(context);
                },
                ),
                Padding(padding: EdgeInsets.all(8.0),),
                GestureDetector(
                child: Text("Camera"),
                onTap: (){
                  _openCamera(context);
                },
                ),
            ],
            ),
        ),
        );
    });
  }

  Widget _decideImageView(){
    if(imageFile == null){
      return Text("No Image Selected");
    } 
    else{
      return Image.file(imageFile, width: 400, height: 400,);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Main Screen"),
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              _decideImageView(),
              RaisedButton(onPressed: (){
                _showChoiceDialog(context);
              },child: Text("Select Image!"),)
            ],
          )
          )
      )
    );
  }
}

我在else部分中检查了是否返回,但问题仍然存在。

我现在已经包括了整个代码。

最佳答案

您必须在其他情况下返回Image Widget:

Widget _decideImageView(){
        if(imageFile == null){
          return Text("No Image Selected");
        } 
        else{
          return Image.file(imageFile, width: 400, height: 400,);
        }
      }

关于flutter - Flutter错误Column的子级不能包含任何空值,但是在索引0处找到了空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58928061/

相关文章:

flutter - flutter/火焰中未定义的类 'TapDownInfo'

generics - Typedef函数中的泛型

dart - 使用带 Dartium 的 `pub serve` 时如何查看 HTML 模板和 CSS 错误?

flutter - 堆栈内小部件的颜色始终稍微透明

flutter - 如果我习惯用 Switch 小部件上的新 val 更改状态,如何使用函数回调?

flutter - getter 'isEmpty'在null上被调用

ios - Flutter IOS 使用连接或 wifi 插件读取 wifi 名称

dart - 如何在Polymer模板中找到HTML元素

firebase - 由于权限问题,无法访问 Firebase 中的 Cloud Firestore

android - 从Flutter项目构建APK时出错