断言失败:第50行pos 15:'image!= null':在Flutter中不为真

时间:2020-09-07 16:55:19

标签: flutter

我正在为应用程序使用flutter,正在使用图像选择器为我的应用程序选择图像,但出现此错误:

失败的断言:第50行pos 15:'image!= null':不正确。在Flutter中

The following assertion was thrown building UserImagePicker(dirty, state: _UserImagePickerState#7f725): 'package:flutter/src/painting/decoration_image.dart': Failed assertion: line 50 pos 15: 'image != null': is not true.

The relevant error-causing widget was: UserImagePicker file:///D:/Flutter/test_app/lib/screens/edit_product_screen.dart:224:21 When the exception was thrown, this was the stack: #2 new DecorationImage (package:flutter/src/painting/decoration_image.dart:50:15) #3 _UserImagePickerState.build (package:test_app/widgets/product_image_picker.dart:57:20) #4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28) #5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15) #6 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11) 

我也尝试过
图片:FileImage(_pickedImage), 然后我收到“文件!=空”错误。

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

class UserImagePicker extends StatefulWidget {
  UserImagePicker(this.imagePickfn);

  final void Function(File pickedImage) imagePickfn;

  @override
  _UserImagePickerState createState() => _UserImagePickerState();
}

class _UserImagePickerState extends State<UserImagePicker> {
  File _pickedImage;

  void _pickImage() async {
    ImageSource source = await showDialog<ImageSource>(
      context: context,
      builder: (context) =>
          AlertDialog(title: Text("Choose image source"), actions: [
        FlatButton(
          child: Text("Camera"),
          onPressed: () => Navigator.pop(context, ImageSource.camera),
        ),
        FlatButton(
          child: Text("Gallery"),
          onPressed: () => Navigator.pop(context, ImageSource.gallery),
        ),
      ]),
    );

    if (source != null) {
      final pickedFile = await ImagePicker().getImage(
        source: source,
        imageQuality: 50,
        maxWidth: 150,
      );
      setState(() => _pickedImage = File(pickedFile.path));
      widget.imagePickfn(_pickedImage);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: <Widget>[
        Container(
          width: 100,
          height: 100,
          margin: EdgeInsets.only(
            top: 8,
            right: 10,
          ),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: _pickedImage != null ? FileImage(_pickedImage) : null,
              fit: BoxFit.cover,
            ),
            border: Border.all(
              width: 1,
              color: Colors.grey,
            ),
          ),
        ),
        FlatButton.icon(
          textColor: Theme.of(context).primaryColor,
          onPressed: () => _pickImage(),
          icon: Icon(Icons.image),
          label: Text('Add an Image'),
        ),
      ],
    );
  }
}

2 个答案:

答案 0 :(得分:0)

 File _image;
 final picker = ImagePicker();

 pickImage()async{
   final pickedFile = await picker.getImage(source: ImageSource.gallery);
   _image = File(pickedFile.path);
  }

答案 1 :(得分:0)

您可以将其上移一级以检查条件。

decoration: BoxDecoration(
     image: _pickedImage != null ?
            DecorationImage(
               image: FileImage(_pickedImage), 
               fit: BoxFit.cover,
            ): null,
 ),

希望有效!