'Future<dynamic>' 类型不是 'Widget' 类型的子类型(以下 _TypeError 在构建 Builder(dirty) 时被抛出:

时间:2021-05-11 20:17:22

标签: android flutter

当我单击按钮或按下按钮时,出现以下错误。我只能导航到下一页,当我返回并按下按钮时,我收到以下错误并且无法导航。 ======== 小部件库捕获的异常 ====================================== ================== 在构建 Builder(dirty) 时抛出了以下 _TypeError: 'Future' 类型不是 'Widget' 类型的子类型

 SubmitArticles() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

  await Firebase.initializeApp();

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  static final String title = 'Please Upload Your Article';
  @override
  Widget build(BuildContext dynamic) => MaterialApp(
    debugShowCheckedModeBanner: false,
    title: title,
    color: Colors.indigo,
    home: MainPage(),
  );
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  UploadTask task;
  File file;

  @override
  Widget build(context) {
    final fileName = file != null ? basename(file.path) : 'No File Selected';
    return WillPopScope(
        onWillPop:()=> Future.value(false),
        child: Scaffold(
          appBar: AppBar(
            backgroundColor:Colors.indigo[900],
        automaticallyImplyLeading:true,
        title: Text(MyApp.title),
        centerTitle: true,
        leading: IconButton(icon: Icon(Icons.arrow_back),
        onPressed:()=>Navigator.push(context,
        MaterialPageRoute<dynamic>(builder: (context)=>UserSubmitOption()))
      )),
      body: Container(
        padding: EdgeInsets.all(32),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ButtonWidget(
                text: 'Select File',
                icon: Icons.attach_file,
                onClicked: selectFile,
              ),
              SizedBox(height: 8),
              Text(
                fileName,
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
              ),
              SizedBox(height: 48),
              ButtonWidget(
                text: 'Upload File',
                icon: Icons.cloud_upload_outlined,
                onClicked: uploadFile,
              ),
              SizedBox(height: 20),
              task != null ? buildUploadStatus(task) : Container(),
            ],
          ),
        ),
      ),
    ));
  }

  Future selectFile() async {
    final result = await FilePicker.platform.pickFiles(allowMultiple: false);

    if (result == null) return;
    final path = result.files.single.path;

    setState(() => file = File(path));
  }

  Future uploadFile() async {
    future: if (file == null) return;

    final fileName = basename(file.path);
    final destination = 'articles/$fileName';

    task = FirebaseApi.uploadFile(destination, file);
    setState(() {});

    future: if (task == null) return;

    final snapshot = await task.whenComplete(() {});
    final urlDownload = await snapshot.ref.getDownloadURL();

    print('Download-Link: $urlDownload');
  }

  Widget buildUploadStatus(UploadTask task) => StreamBuilder<TaskSnapshot>(
    stream: task.snapshotEvents,
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        final snap = snapshot.data;
        final progress = snap.bytesTransferred / snap.totalBytes;
        final percentage = (progress * 100).toStringAsFixed(2);

        return Text(
          '$percentage %',
          style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
        );
      } else {
        return Container();
      }
    },
  );
}

0 个答案:

没有答案