Home > Blockchain >  type 'List<dynamic>' is not a subtype of type 'List<Widget>' How do
type 'List<dynamic>' is not a subtype of type 'List<Widget>' How do

Time:10-06

I am trying to use a list in a Listview widget in flutter but I keep getting an error saying that I need a List[Widget]. All the answers online make use of maps and I am still a beginner to Flutter. Can anyone show me how to use the map function for this or a way to convert List [Dynamic] to List [Widget]?

Here is my code:

import 'package:flutter/material.dart';

class NextPage extends StatefulWidget {
  final List value;

  NextPage({Key key, this.value}) : super(key: key);

  @override
  _NextPageState createState() => new _NextPageState();
}

class _NextPageState extends State<NextPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Reminders"),
      ),
      body: ListView(
        children: widget.value,
      ),
    );
  }
}

CodePudding user response:

According to the discussion, value is List<String> in that case, to pass it on ListView we need to use these data and convert them into widget.

Here we are using value and making Text widget with it


import 'package:flutter/material.dart';

class NextPage extends StatefulWidget {
  final List<String> value;

  NextPage({Key key, this.value}) : super(key: key);

  @override
  _NextPageState createState() => new _NextPageState();
}

class _NextPageState extends State<NextPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Reminders"),
      ),
      body: ListView(
       children: widget.value.map((e) => Text(e)).toList(),
      ),
    );
  }
}

Does it solve the issue?

  • Related