2021年3月10日星期三

.toList() is Optional. Why?

In this code I can delete .toList() and get the same output. Why is it optional?
(My theory is because the spread operator is converting the output to a list anyway so .toList isn't doing anything...like calling .toString on a string.)

If I delete the spread operator and leave .toList() I get the error "type List<dynamic> is not a subtype of type Widget".
(This invalidates my theory the spread operator and .toList are having the same effect)

I read the previously mentioned error was due to type inference and saw suggestions to put <Widget> immediately after .map like so: snapshot.data.docs.map<Widget>((e)... however I get a similar error: List<Widget> is not a subtype of type Widget.

I'm not asking for anyone to solve a code problem. It's just that I've been coding for less than three months...I'm more looking for an explanation dumbed down enough for a newbie like me to understand.

import 'package:cloud_firestore/cloud_firestore.dart';  import 'package:flutter/material.dart';    import 'package:ml_app/widgets/shared/loading.dart';    class TodaysReviewScreen2 extends StatefulWidget {    @override    _TodaysReviewScreen2State createState() => _TodaysReviewScreen2State();  }    class _TodaysReviewScreen2State extends State<TodaysReviewScreen2> {    @override    Widget build(BuildContext context) {      return StreamBuilder(        stream: FirebaseFirestore.instance.collection('entries').snapshots(),        builder: (ctx, snapshot) {          if (snapshot.connectionState == ConnectionState.waiting) {            return Loading();          }          return ListView(            padding: const EdgeInsets.all(8),            children: <Widget>[              Container(                child: Column(children:[                   ...snapshot.data.docs.map<Widget>((e) => Text('${e['title']}')).toList(),                ]),              ),            ],          );        },      );    }  }                
https://stackoverflow.com/questions/66575490/tolist-is-optional-why March 11, 2021 at 10:05AM

没有评论:

发表评论