【问题标题】:error: A value of type 'Widget' can't be assigned to a variable of type 'File'错误:“Widget”类型的值不能分配给“File”类型的变量
【发布时间】:2019-08-08 11:22:10
【问题描述】:

'File _image'在类State中声明,编码意味着在Widget body中声明'null -> image'(同步?)。但这种编码是不合适的。 我该怎么办?

我什么都做不了。

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

class CreatePage extends StatefulWidget {
..@override
...._CreatePageState createState() => _CreatePageState();
}

class _CreatePageState extends State<CreatePage> {
..final textEditingController = TextEditingController();
..File _image;
..@override
..void dispose() {
....textEditingController.dispose();
....super.dispose();
..}
..@override
..Widget build(BuildContext context) {
....return Scaffold(
......appBar: _buildAppBar(),
......body: _buildBody(),
......floatingActionButton: FloatingActionButton(onPressed: _getImage,
......child: Icon(Icons.add_a_photo),
....),
....);
..}

..Widget _buildAppBar() {
....return AppBar(
......actions: <Widget>[
........IconButton(
..........icon: Icon(Icons.send), onPressed: () {},
..)
..],
..);
..}

Widget _buildBody() {
..return SingleChildScrollView(
.....child: Column(
.....children: <Widget>[
_image = null ? Text('No image'): Image.file(_image), // problem coding
.....TextField(
.....decoration: InputDecoration(hintText: 'write context'),
.....controller: textEditingController,
..)
..],
..),
..);
..}

Future _getImage() async {
..File image = await ImagePicker.pickImage(source: ImageSource.gallery);
..setState(() {
.._image = image;
..});
..}
..}

我看到了图片的选择,比如 instargram 的文字。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    替换

    _image = null ? Text('No image'): Image.file(_image), // problem coding
    

    _image == null ? Text('No image'): Image.file(_image), // problem solved
    

    【讨论】:

    • omg....... 我从来不知道 TTTTTTTTTTT 非常感谢!!上帝保佑你10000000年TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
    • 对于新手(比如我):_image = null_image 的值设置为null,而_image == null(两个等号)检查@987654327 的值是否@是null
    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 2021-07-23
    • 2023-01-20
    • 2020-02-11
    • 1970-01-01
    • 2020-07-02
    • 2021-03-28
    • 2021-07-05
    相关资源
    最近更新 更多