Flutter 'Future <void>'는 '위젯'유형의 하위 유형이 아닙니다. [중복]

Nov 29 2020

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();
       )
);

이것이 가능한가 ?

답변

1 ShriHari Nov 29 2020 at 20:18

전화 loadAssets()에서 initState()작동합니다. 선택 후 이미지가 images목록에 저장됩니다 . 그런 다음 원하는 것을하십시오.

문서 에서 가져온 예 ,

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")),
            )
          ],
        ),
      ),
    );
  }
}