Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Data from FutureProvider prints two different variables who have same value for an instance only once , why?

I have a data model Product whose data is stored in firestore. When I read its data using Provider and print the fields of product dim_x and dim_y, I only get values printed which are different. For example,

I have 2 products
P1 with dim_x = 200.0 and dim_y = 100.0
P2 with dim_x = 80.0 and dim_y = 80.0

The print command in my code below prints 80.0 only once, why is that?
({200.0, 100.0}, {80.0})
why not ({200.0, 100.0}, {80.0, 80.0}) is printed?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Product {
  final String pid;
  final double dim_x;
  final double dim_y;
  
  Product(
      {required this.pid,
      required this.dim_x,
      required this.dim_y,})
}

class CreateJob extends StatefulWidget {
  CreateJob({super.key});

  @override
  State<CreateJob> createState() => _CreateJobState();
}

class _CreateJobState extends State<CreateJob> {

Future<List<Product>> fetchProductsFromBase() async {
  // .. code for getting data from firestore collection
}

@override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        FutureProvider<List<Product>>(create: (_) => fetchProductsFromBase(), initialData: []),
        // ...,
      ],
      child: SafeArea(
//...somewhere down the widget tree
//ScheduledDelivery()
}

class ScheduledDelivery extends StatefulWidget {

  const ScheduledDelivery({super.key});

  @override
  State<ScheduledDelivery> createState() => _ScheduledDeliveryState();
}

class _ScheduledDeliveryState extends State<ScheduledDelivery> {
  late List<Product> productList;

  @override
  void initState() {
    super.initState();
    productList = context.read<List<Product>>();
  }

  @override
  Widget build(BuildContext context) {
    print('\n${productList.map((e) => {e.dim_x, e.dim_y})}\n');
// ...

}

>Solution :

This is a Set: {e.dim_x, e.dim_y}. Set cannot have duplicate value.

You might be looking for Record instead.

Change {e.dim_x, e.dim_y} to (e.dim_x, e.dim_y). Or just use a proper string interpolation to print the values.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading