簡體   English   中英

飛鏢/顫振 - Class '基准' 沒有實例方法 '[]'

[英]dart/flutter - Class 'Datum' has no instance method '[]'

我有一個 Podo class,它從 API 獲取數據。 那工作正常。 在下拉字段中,我使用 id 的值並顯示名稱。

來自 api 響應的示例 Json 數據如下所示:

{"jsonapi":{"version":"1.0"},"data":[{"type":"testkit","id":"1","attributes":{"active":true,"testType":"type1","code":"T101","name":"TestName1","infos":{},}},{"type":"testkit","id":"2","attributes":{"active":true,"testType":"type2","code":"T102","name":"TestName2","productId":null,"infos":{}}}]}

對於快照:

child: FutureBuilder(
       future: _testkit,
       builder: (context,
       AsyncSnapshot<TestkitList> snapshot) {

drowdownFieldWidget 代碼如下:

DropdownButtonFormField<String>(
hint:Text("Select Testkit Name"),
value: _selectedTestkit,
                onChanged: (newValue) {
                    setState(() {
                    _selectedTestkit = newValue;
_selectedTestType = getTestType()// this I am trying to get the value of test type, but not working.

                    });
                },
                validator: (value) => value ==
                        null
                    ? 'Please select the Testkit'
                    : null,
                items: (snapshot.data.data)
                    .map((item) =>
                        DropdownMenuItem<
                            String>(
                            child: Text(
                            item.attributes.name
                                        .length >
                                    30
                                ? item
                                    .attributes
                                    .name
                                    .substring(
                                        0, 30)
                                : item
                                    .attributes
                                    .name,
                            ),
                            value: item.id,
                        ))
                    .toList(),
                );
            }
        }
        }),
)),

現在除了選中項的 id 外,我還需要訪問attributestestType的值

我嘗試通過創建 function getTestType()來訪問該值,如下所示:

getTestType() async {    

    final List testkits = await responsetestkit.then((value) => value.data);
    print("below line is printed in function getTestType");
    print(testkits.runtimeType);
    
    print(testkits);
    var testtype =
        testkits.firstWhere((testkit) => testkit["id"] == _selectedTestkit);  

    print(testtype);
    
  }

此 function 返回錯誤 - Class 'Datum' has no instance method '[]'.

如果需要任何進一步的信息,請告訴我。

更新:Podo 文件的內容:

import 'dart:convert';

TestkitList testkitListFromMap(String str) => TestkitList.fromMap(json.decode(str));

String testkitListToMap(TestkitList data) => json.encode(data.toMap());

class TestkitList {
    TestkitList({
        this.jsonapi,
        this.data,
    });

    Jsonapi jsonapi;
    List<Datum> data;

    factory TestkitList.fromMap(Map<String, dynamic> json) => TestkitList(
        jsonapi: json["jsonapi"] == null ? null : Jsonapi.fromMap(json["jsonapi"]),
        data: json["data"] == null ? null : List<Datum>.from(json["data"].map((x) => Datum.fromMap(x))),
    );

    Map<String, dynamic> toMap() => {
        "jsonapi": jsonapi == null ? null : jsonapi.toMap(),
        "data": data == null ? null : List<dynamic>.from(data.map((x) => x.toMap())),
    };
}

class Datum {
    Datum({
        this.type,
        this.id,
        this.attributes,
    });

    String type;
    String id;
    Attributes attributes;

    factory Datum.fromMap(Map<String, dynamic> json) => Datum(
        type: json["type"] == null ? null : json["type"],
        id: json["id"] == null ? null : json["id"],
        attributes: json["attributes"] == null ? null : Attributes.fromMap(json["attributes"]),
    );

    Map<String, dynamic> toMap() => {
        "type": type == null ? null : type,
        "id": id == null ? null : id,
        "attributes": attributes == null ? null : attributes.toMap(),
    };
}

class Attributes {
    Attributes({
        this.active,
        this.testType,
        this.code,
        this.name,        
        this.infos,
        
    });

    bool active;
    String testType;

首先,您將getTestType方法標記為async ,然后它只返回一個Future並且需要thenawait來獲取值:

onChanged: (newValue) async {
  _selectedTestkit = newValue; 
  _selectedTestType = await getTestType();
  setState(() {});
},

其次, getTestType方法不返回任何值。 修復添加返回值:

getTestType() async {    
  final List testkits = await responsetestkit.then((value) => value.data);
  print("below line is printed in function getTestType");
  print(testkits.runtimeType);
  
  print(testkits);
  var testtype =
      testkits.firstWhere((testkit) => testkit["id"] == _selectedTestkit);  

  print(testtype);
  return testtype;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM