如何在flutter中返回一个项目和项目列表

时间:2019-03-25 05:31:42

标签: dart flutter

我构建了一个应用程序,该应用程序用于从服务器返回用户信息,该信息可能包含项目或项目列表 我的问题是,当我将变量定义为列表时,它会返回项目列表,但是当我只想返回一个项目时会出错,而当我将变量定义为映射时,它会返回一个项目但返回时却出错一个列表 我该如何解决?

P.S。这是我的代码

import 'package:flutter/material.dart';
import 'package:idb/pages/aboutus.dart';
import 'package:idb/pages/logout.dart';
import 'package:idb/utilities/cardinfo.dart';
import 'package:idb/utilities/getTokenParameter.dart';
import 'package:date_range_picker/date_range_picker.dart' as DateRagePicker;
import 'package:idb/utilities/postRequest.dart';

String userid = '0000';

Future getUsername() async {
  var tokeninfo = await parseJwt();
  userid = tokeninfo['117115101114105100'];
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  List item;

  cardfoo() async {
    Map<String, dynamic> user = await getCards(userid);

    item = user['ResponseData']['data']['ROW'];

    if (mounted) {
      setState(() {
        item = user['ResponseData']['data']['ROW'];

        print('item: ${item}');
      });
    }
    return 'success';
  }

  getTransactions(
      String cardLast4digits, String dateFrom, String dateTo) async {
    var tokeninfo = await parseJwt();
    print(tokeninfo);
    print(cardLast4digits);

    String userid = tokeninfo['117115101114105100'];
    String username = tokeninfo['11711510111411097109101'];
    String url =
        'http://192.168.128.130:';

    final Map<String, dynamic> authData = {
      'Userid': userid,
      'Username': username,
      'Last4Digit': cardLast4digits,
      'DateFrom': dateFrom,
      'DateTo': dateTo,
    };

    print('befor post: ${authData}');

    Map<String, dynamic> responseData = await post(url, authData);

    print(responseData);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Home"),
        ),
        body: item == null
            ? Center(
                child: CircularProgressIndicator(),
              )
            : ListView.builder(
                itemCount: item == null ? 0 : item.length,
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemBuilder: (BuildContext context, int index) {
                  return new Column(
                    children: <Widget>[
                      Card(
                        margin: EdgeInsets.only(
                          top: 20,
                          left: 15,
                          right: 15,
                        ),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(15.0),
                        ),
                        color: Colors.white70,
                        child: new Column(
                          children: <Widget>[

                            new Padding(
                                padding: new EdgeInsets.all(7.0),
                                child: new Column(
                                  children: <Widget>[
                                    new Row(children: <Widget>[
                                      new Padding(
                                        padding: new EdgeInsets.all(7.0),
                                        child: new Text('Embossed Name:',
                                            style: new TextStyle(
                                                fontSize: 15.0,
                                                color: Colors.orange)),
                                      ),
                                      new Padding(
                                        padding: new EdgeInsets.all(7.0),
                                        child: new Text(
                                            item[index]['EMBOSSED_NAME'],
                                            style:
                                                new TextStyle(fontSize: 15.0)),
                                      ),
                                    ]),


                      SizedBox(
                        height: 10,
                      ),
                      Column(
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              FlatButton.icon(
                                textColor: Colors.blue,
                                icon: Icon(
                                    Icons.calendar_today), //`Icon` to display
                                label: Text('Transctions'),

                                onPressed: () async {
                                  final List<DateTime> picked =
                                      await DateRagePicker.showDatePicker(
                                          context: context,
                                          initialFirstDate: new DateTime.now(),
                                          initialLastDate: (new DateTime.now())
                                              .add(new Duration(days: 7)),
                                          firstDate: new DateTime(2015),
                                          lastDate: new DateTime(2020));

                                  if (picked != null && picked.length == 2) {

                                    getTransactions(
                                        item[index]['CARD_NO'],
                                        picked[0].toString(),
                                        picked[1].toString());
                                  }
                                },
                              ),
                            ],
                          ),
                        ],
                      ),
                    ],
                  );
                },
              ));
  }

  @override
  void initState() {
    super.initState();
    this.cardfoo();
  }
}

错误是

  

Dart错误:未处理的异常:类型'_InternalLinkedHashMap'不是类型'List'的子类型

1 个答案:

答案 0 :(得分:0)

您在这里出错:

 Map<String, dynamic> user = await getCards(userid);

那是因为getCards返回一个List而不是一个Map,因此请尝试获取第一个元素,例如。

  Map<String, dynamic> user = (await getCards(userid))[0];