Flutter'Future<void>'不是'Widget'类型的子类型[重复]

Posted

技术标签:

【中文标题】Flutter\'Future<void>\'不是\'Widget\'类型的子类型[重复]【英文标题】:Flutter 'Future<void>' is not a subtype of type 'Widget' [duplicate]Flutter'Future<void>'不是'Widget'类型的子类型[重复] 【发布时间】:2021-03-11 14:19:52 【问题描述】:

我必须使用 MultiImagePicker 选择图像,所以我有这个等待所选资产的函数,但是当我点击一个按钮时会调用这个函数。 我想在不点击任何按钮的情况下进入页面时选择照片,这可能吗?

List<Asset> images = List<Asset>();
Future<void> loadAssets() async 
List<Asset> resultList = List<Asset>();
String error = '';
try 
  resultList = await MultiImagePicker.pickImages(
    maxImages: 300,
    enableCamera: false,
    selectedAssets: images,
  );
 on Exception catch (e) 
  error = e.toString();

if (!mounted) return;

setState(() 
  images = resultList;
);


@override
Widget build(BuildContext context) 
if(images.length == 0)
return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
              centerTitle: true,
              title: Text('Picker Example'),
              backgroundColor: Colors.blueAccent
          ),
          body:Container(
            child: images.length > 0 ? Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  child: Text("Select photo"),
                  onPressed: () => loadAssets(),
                ),
                RaisedButton(
                  child: Text("Remove photo"),
                  onPressed: removeAssets,
                ),
                RaisedButton(
                  child: Text("upload photo"),
                  onPressed: upload,
                ),
                Expanded(
                  child: buildGridView(),
                )
              ],
            ) :loadAssets();
       )
);

这可能吗?

【问题讨论】:

在这里查看示例:pub.dev/packages/multi_image_picker/example 已经看到了,我说我不想在没有点击任何按钮的情况下这样做,所以我不想在没有按下的情况下加载资产 那么,您想在没有按钮的页面加载时选择图像吗? 是的,如果我选择一些图像,我不想看到两个按钮用于上传和删除之前选择的照片和重新选择照片,我已经这样做了,现在我更新了我的问题你可以更好地理解 【参考方案1】:

initState() 函数中调用loadAssets()。选择后,图像存储在images 列表中。然后做你想做的事。

示例取自Docs,

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

import 'package:multi_image_picker/multi_image_picker.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget 
  @override
  _MyAppState createState() => new _MyAppState();


class _MyAppState extends State<MyApp> 
  List<Asset> images = List<Asset>();
  String _error = 'No Error Dectected';

  @override
  void initState() 
    super.initState();
    loadAssets();
  

  Widget buildGridView() 
    return GridView.count(
      crossAxisCount: 3,
      children: List.generate(images.length, (index) 
        Asset asset = images[index];
        return AssetThumb(
          asset: asset,
          width: 300,
          height: 300,
        );
      ),
    );
  

  Future<void> loadAssets() async 
    List<Asset> resultList = List<Asset>();
    String error = 'No Error Dectected';

    try 
      resultList = await MultiImagePicker.pickImages(
        maxImages: 300,
        enableCamera: true,
        selectedAssets: images,
        cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
        materialOptions: MaterialOptions(
          actionBarColor: "#abcdef",
          actionBarTitle: "Example App",
          allViewTitle: "All Photos",
          useDetailsView: false,
          selectCircleStrokeColor: "#000000",
        ),
      );
     on Exception catch (e) 
      error = e.toString();
    

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() 
      images = resultList;
      _error = error;
    );
  

  @override
  Widget build(BuildContext context) 
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: <Widget>[
            Center(child: Text('Error: $_error')),
            Expanded(
              child: images.length > 0 ? buildGridView() : Center(child: Text("No Images")),
            )
          ],
        ),
      ),
    );
  

【讨论】:

哟 Hari,你知道如何解决这个问题吗? ***.com/questions/67705273/…

以上是关于Flutter'Future<void>'不是'Widget'类型的子类型[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Promise 回调参数类型不可分配给 '(value: unknown) => void | PromiseLike<void>'

对于操作计数()。 std::set<void*> 或 std::unordered_set<void*> 哪个更快?

如何将返回 `void` 的同步方法包装到 Mono<Void> 中?

Spring Boot RestController 中返回 void 和 ResponseEntity<Void> 有啥区别?

返回 Mono<Void> 后的方法调用

Alamofire 库 Void 不能转换为 Response<AnyObject, NSError> -> Void