Home > Enterprise >  A value of type 'List<Celebrity>?' can't be assigned to a variable of type 
A value of type 'List<Celebrity>?' can't be assigned to a variable of type 

Time:09-05

class _CelebrityListState extends State<CelebrityList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Liste des célébrités actuels'),
      ),
      body: FutureBuilder<List<Celebrity>>(
          future: CelebrityDatabase.instance.celebritys(),
          builder:
              (BuildContext context, AsyncSnapshot<List<Celebrity>> snapshot) {
            if (snapshot.hasData) {
              List<Celebrity> celebritys = snapshot.data;
              return ListView.builder(
                itemCount: celebritys.length,
                itemBuilder: ((context, index) {
                  final celebrity = celebritys[index];
                  return AnimatedCard(
                      direction: AnimatedCardDirection.left,
                      initDelay: const Duration(milliseconds: 0),
                      duration: const Duration(seconds: 1),
                      onRemove: () {
                        setState(() {
                          celebritys.removeAt(index);
                        });
                        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                            content: Text('${celebrity.name} supprimé.')));
                      },
                      child: CelebrityListItem(celebrity: celebrity));
                }),
              );
            } else {
              return const Text('No data');
            }
          }),
    );
  }
}

The problem is in List celebritys = snapshot.data; When I hover it , it show : A value of type 'List?' can't be assigned to a variable of type 'List'. Try changing the type of the variable, or casting the right-hand type to 'List'.

CodePudding user response:

Since you check if snapshot has data, you can change the line to the follwing instead:

List<Celebrity> celebritys = snapshot.data!;
  • Related