Home > Back-end >  flutter addAll isn't defined for Iterable
flutter addAll isn't defined for Iterable

Time:12-19

I need your help for the following problem on line 22. Flutter says, that "addAll isn't defined for Iterable". What do I need to change in my code or do you need additional Information?

import 'package:flutter/material.dart';
import 'package:MyApp/jsonApi/dataModels/dataModelPlaces.dart';
import 'package:MyApp/jsonApi/parsers/repositoryPlaces.dart';

class ShareAppScreen extends StatefulWidget {
  @override
  _ShareAppScreenState createState() => _ShareAppScreenState();
}

class _ShareAppScreenState extends State<ShareAppScreen> {
  //List<DataModelPlaces> _places = <DataModelPlaces>[];
  Iterable<DataModelPlaces> _places = <DataModelPlaces>[];
  Iterable<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];

  bool _isPlacesLoading = false;

  @override
  void initState() {
    super.initState();
    fetchPlaces().then((pvalue) {
      setState(() {
        _isPlacesLoading = false;
        _places.addAll(pvalue); //<- here I have a problem that addAll isn't defined for Iterable
        _placesDisplay = _places;
        print('User display pin Records: ${pvalue.data!.length}');
        var i=0;
        while (i < pvalue.data!.length){
          print('User display Lat of $i: ${pvalue.data![i].attributes!.latitude}');
          print('User display Long of $i: ${pvalue.data![i].attributes!.latitude}');
          i  ;
        }
      });
    });
  }

  List stocksList = [
    CompanyStocks(name: "Intel Corp", price: 56.96),
    CompanyStocks(name: "HP Inc", price: 32.43),
    CompanyStocks(name: "Apple Inc", price: 133.98),
    CompanyStocks(name: "Microsoft Corporation", price: 265.51)
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Users List ${_places.length}'),
      ),
      body: SafeArea(
        child: Container(

          child: ListView.builder(
            itemCount: stocksList.length,
            itemBuilder: (context, index) {
              return Card(
                child: Padding(
                  padding: EdgeInsets.all(10),
                  child: ListTile(
                    title: Text(
                      stocksList[index].name,
                        style: TextStyle(
                        fontSize: 20,
                      ),
                    ),
                    leading: CircleAvatar(
                      child: Text(
                        stocksList[index].name[0],
                        style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                      ),
                    ),
                    trailing: Text("\$ ${stocksList[index].price}"),
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

class CompanyStocks {
  String name;
  double price;

  CompanyStocks({required this.name,required this.price});
}

At the End I would need an Variable "_places" and "_placesDisplay" of DataModelPlaces which I can use in in Place of the List "stocksList" which is working but not _places / _placesDisplay"

Many Thanks

Roman

CodePudding user response:

Iterable does not have .addAll. You need to convert it to a List first so you can addAll the elements to it such as:

Update: My bad. toList() will return a new list! You should try the alternative approach below

// This wrong since it will return a new list (wrong)
// _places.toList().addAll(pvalue)

Alternatively, you can change the definition to be List instead of Iterable:

from:

Iterable<DataModelPlaces> _places = <DataModelPlaces>[];
Iterable<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];

To:

List<DataModelPlaces> _places = <DataModelPlaces>[];
List<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];

Update:

As discussed in the comments, you want to make sure that fetchPlaces is returning an Iterable in order to use _places.addAll(pvalue) otherwise, if it's a single object, use _places.add(pvaule).

  • Related