简体   繁体   中英

While I am retrieving my array of document in cloud firestore using Flutter I got error like list is not a sub type of string

Here is my dart code:

import 'dart:io';

import 'chewie_list_item.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:video_player/video_player.dart';

void main() {
  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.dumpErrorToConsole(details);
    if (kReleaseMode) exit(1);
  };

  runApp(MaterialApp(
    home: CourseApp(),
  ));
}

class CourseApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Flutter Demo"),
        ),
        body: StreamBuilder(
          stream: Firestore.instance.collection("courses").snapshots(),
          builder: (context, snapshot) {
            return ListView.builder(
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) {
                DocumentSnapshot courses = snapshot.data.documents[index];
                return (ChewieListItem(
                  videoPlayerController: VideoPlayerController.network(
                      courses['video'] ?? 'default'),
                ));
              },
            );
          },
        ));
  }
}

This is the error message I get while running the app and this is my firestore structure .

I believe what is causing the issue is that courses['video'] is an array, as per what you shared in your screenshot of the firestore, and the VideoPlayerController.network() is expecting a string, so you have to either change that function, use a single value of that array or concatenate the values of the array into a single string, which is what I have done in this untested code, give it a try:

@override
Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Flutter Demo"),
        ),
        body: StreamBuilder(
            stream: Firestore.instance.collection("courses").snapshots(),
            builder: (context, snapshot) {
                return ListView.builder(
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (context, index) {
                        DocumentSnapshot courses = snapshot.data.documents[index];
                        var video = courses['video'];
                        var str = "";
                        video.foreach(value => {
                            str = str + value + ", "
                        })
                        // to remove the last ", "
                        str = str.substring(0, str.length() - 2);
                        return (ChewieListItem(videoPlayerController: VideoPlayerController.network(str ?? 'default')));
                    },
                );
            },
        )
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM