Home > Back-end >  How to solve "The return type 'List<Text>?' isn't a 'Widget', as
How to solve "The return type 'List<Text>?' isn't a 'Widget', as

Time:09-18

im try to solve this problem i think the problem is trying to convert Text widget to List. But i don't know what to do. i searched on internet but i could find solution.

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

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

  class _HomeScreenState extends State<HomeScreen> {
    var stream = FirebaseFirestore.instance.collection("Notes");
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(),
        body: Column(
          children: [
            StreamBuilder<QuerySnapshot>(
              stream: stream.snapshots(),
              builder: (context, snapshot) {
                var docsList = snapshot.data?.docs;
                return Expanded(
                  child: ListView.builder(
                    itemCount: snapshot.data?.docs.length,
                    itemBuilder: (BuildContext context, int index) {
                      return docsList?.map((document) => Text(document["title"])).toList();
                    },
                  ),
                );
              },
            ),
          ],
        ),
      );
    }
  }

CodePudding user response:

You are trying to return a list of widget from itemBuilder, but it expect to return single widget. If you like to have multiple widget, you can use Column widget,

itemBuilder: (BuildContext context, int index) {
  return Column(
    children: docsList
            ?.map((document) => Text(document["title"]))
            .toList() ??
        [],
  );
},

Or you might just want

itemBuilder: (BuildContext context, int index) {
  return Text(docsList?[index]["title"]);
},

CodePudding user response:

Use

itemBuilder: (BuildContext context, int index) {
            return ListTile(
                leading: const Icon(Icons.list),
                trailing: const Text(
                  "TRAIL",
                  style: TextStyle(color: Colors.green, fontSize: 15),
                ),
                title: Text("${snapshot.data?.docs[index]["title"]}"));
          }),
  • Related