简体   繁体   English

Flutter:打印列表如何来自提供商或将其作为邮件发送到 API 编辑#1

[英]Flutter : How can print list comes from provider or send it as post to API Edit #1

I have flutter app works fine,first we have Product class:我有 flutter 应用程序工作正常,首先我们有产品 class:

class Products {
  final String item;
  final int price;

  Products({this.item, this.price});

  factory Products.fromMap(Map<String, dynamic> json) => Products(
        item: json["item"],
        price: json["price"],
      );

  Map<String, dynamic> toMap() => {
        "item": item,
        "price": price,
      };
}

also I have page contain list of products:我也有页面包含产品列表:

import 'package:bexshin/service/provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'models/products.dart';

class ProductList extends StatelessWidget {
  @override
  List<Products> myProducList;
  Widget build(BuildContext context) {
    myProducList = [
      Products(item: 'Car', price: 20000),
      Products(item: 'PC', price: 500),
      Products(item: 'LapTop', price: 750),
      Products(item: 'House', price: 25000),
    ];
    return Scaffold(
      body: ListView.builder(
          shrinkWrap: true,
          itemCount: myProducList.length,
          itemBuilder: (context, index) {
            return Card(
              child: ListTile(
                title: Text(myProducList[index].item),
                subtitle: Text(myProducList[index].price.toString()),
                trailing: Consumer<MyProv>(
                  builder: (context, myProv, child) {
                    return IconButton(
                      icon: Icon(Icons.add),
                      onPressed: () {
                        myProv.add_item(myProducList[index]);
                      },
                    );
                  },
                ),
              ),
            );
          }),
    );
  }
}

also I have provider like this:我也有这样的提供者:

import '../models/products.dart';
import 'package:flutter/material.dart';

class MyProv extends ChangeNotifier {
  List<Products> product = [];

  void add_item(item) {
    product.add(item);
    notifyListeners();
  }

  void del_item(item) {
    product.remove(item);
    notifyListeners();
  }

  get myProduct => product;
  get myMap => myProduct.toMap();
}

and I have favorite page which view list of products added to Wishlist:我有最喜欢的页面,其中查看添加到愿望清单的产品列表:

import 'dart:convert';
import 'package:bexshin/service/provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';    
import 'models/products.dart';

class Favor extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Consumer<MyProv>(builder: (context, myProv, child) {
      return Column(
        children: [
          ListView.builder(
              shrinkWrap: true,
              itemCount: myProv.myProduct.length,
              itemBuilder: (context, index) {
                return Card(
                  child: ListTile(
                    title: Text('${myProv.myProduct[index].item}'),
                    subtitle: Text('${myProv.myProduct[index].price}'),
                    trailing: IconButton(
                      icon: Icon(Icons.remove),
                      onPressed: () {
                        myProv.del_item(myProv.myProduct[index]);
                      },
                    ),
                  ),
                );
              }),
          RaisedButton(onPressed: () {
            var body = json.encode(myProv.myMap); // ------ I want the action here
            print(body);
          }),
        ],
      );
    }));
  }
}

Now I want print the Wishlist (myProv.myProduct),How can I do that?现在我想打印愿望清单(myProv.myProduct),我该怎么做? what id its gave me error:它给了我什么错误:

Class 'List<Products>' has no instance method 'toMap'.

also I want to send the Wishlist (myProv.myProduct) to an API,how can I use the Wishlist for that?我还想将愿望清单 (myProv.myProduct) 发送到 API,我该如何使用愿望清单?

Handle your object as a map for printing and updating in the API将您的 object 作为 map 处理,以便在 API 中打印和更新

add this helper in your model在您的 model 中添加此助手

Map<String, dynamic> toMap() => {
'item': item,
'price': price,
};

and print and update it like this并像这样打印和更新它

product.toMap()

I solved it by using fromIterable:我通过使用 fromIterable 解决了它:

Map<String, int> result = Map.fromIterable(myProv.myProduct,
                    key: (v) => v.item, value: (v) => v.price);
                print(result);

the Output: Output:

{Car: 20000, PC: 500, LapTop: 750, House: 25000}

Or use this:或者使用这个:

var result = myProv.myProduct.map((item) {
                  return {"item": item.item, "price": item.price};
                }).toList();

the Output: Output:

[
 {item: Car, price: 20000},
 {item: PC, price: 500},
 {item: LapTop, price: 750},
 {item: House, price: 25000}
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM