【问题标题】:Flutter error Column's children must not contain any null values, but a null value was found at index 0Flutter 报错 Column 的子项不能包含任何空值,但在索引 0 处发现了空值
【发布时间】:2019-11-19 06:43:31
【问题描述】:

我在 Flutter 中尝试了一个相机应用程序,遇到了列的子级不能包含空值的问题,但在索引 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 部分检查了有无返回,但问题仍然存在。

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

【问题讨论】:

  • 确保 imageFile 是文件系统上的有效位置,然后在 else 条件下返回您的小部件。
  • 在 else 语句中返回您的 Image 小部件,您能否展示您的 _showChoiceDialog 方法?

标签: flutter dart


【解决方案1】:

您必须在 else 情况下返回您的图像小部件:

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

【讨论】:

  • 我已经尝试过了,但问题仍然存在。我现在已经包含了我的整个代码,请看一下。
【解决方案2】:

只需在 _decideImageView() 函数的 else 部分的 Image.File 之前添加一个返回即可。

您必须记住,您调用的函数_decideImageView() 必须返回一些内容。因此,当您的 imageFile 不为 null 时,它将转到 else 但不返回。这就是为什么您在列中收到空索引错误

【讨论】:

  • 我已经尝试过了,但问题仍然存在。我现在已经包含了我的整个代码,请看一下。
  • 抱歉迟到了,它是固定的还是你面临的。请告诉我!
猜你喜欢
  • 1970-01-01
  • 2022-12-03
  • 2020-12-08
  • 2020-12-02
  • 1970-01-01
  • 2021-06-10
  • 2021-10-29
  • 2020-03-04
  • 2021-07-31
相关资源
最近更新 更多