【问题标题】:flutter error: _InternalLinkedHashMap<String, dynamic>颤振错误:_InternalLinkedHashMap<String, dynamic>
【发布时间】:2021-08-29 18:24:29
【问题描述】:

Error-> 类 '_InternalLinkedHashMap' 没有实例 getter 'psubCatName'。 接收方:_LinkedHashMap 长度:9 尝试调用:psubCatName

这是我的 TabPage,我正在尝试使用地图在 TabBar 的选项卡内获取数据。

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:hospital/customApiVariable.dart';
import 'package:http/http.dart' as http;

class TabPage extends StatefulWidget {
  final medicineCatUniqId;
  const TabPage({Key key, this.medicineCatUniqId}) : super(key: key);

  @override
  _TabPageState createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> {
  var response;


  var medicineSubCategoryApi;

  @override
  void initState() {
    // TODO: implement initState
    //
    super.initState();
// for loading
    fetchData(widget.medicineCatUniqId);
  }

  fetchData(var medicineCatUniqId) async {
    var api = Uri.parse('$baseUrl/productSubCatApi.php?a2rTokenKey=$a2rTokenKey&pcat=$medicineCatUniqId');
    response = await http.get(
      api,
    );
    print("medicineCatApiLnk " + api.toString());
    print("medicineCat" + response.body);
    medicineSubCategoryApi = jsonDecode(response.body);

    print("medicineCatString" + medicineSubCategoryApi.toString());
    return medicineSubCategoryApi;
    // setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MaterialApp(
        home: ListView(
          children: [
            FutureBuilder(
                future: fetchData(medicineSubCategoryApi),
                // initialData: medicineSubCategoryApi,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {

                    print(snapshot.data.length.toString());
                    print(snapshot.data.toString());
                    // return Column(
                    //   crossAxisAlignment: CrossAxisAlignment.center,
                    //   children: [
                    //     Text(snapshot.data.length.toString()),
                    //     Text(snapshot.data.toString()),
                    //   ],
                    // );

                    // API data will be stored as snapshot.data
                    return DefaultTabController(
                      length: snapshot.data.length,
                      child: Scaffold(
                        appBar: AppBar(
                          title: const Text('Tabbed AppBar'),
                          bottom: TabBar(
                            isScrollable: true,
                            tabs: snapshot.data
                                .map((choice) => Tab(
                                      text: choice.psubCatName,
                                      // icon: Icon(choice),
                                    ))
                                .toList(),
                          ),
                        ),
                        body: TabBarView(
                          children: snapshot.data.map<Widget>((choice) {
                            return Padding(
                              padding: const EdgeInsets.all(20.0),
                              child: ChoicePage(
                                choice: choice,
                              ),
                            );
                          }).toList(),
                        ),
                      ),
                    );
                  } else if (snapshot.hasError) {
                    return Text('Error');
                  } else {
                    return Text('Loading');
                  }
                }),
          ],
        ),
      ),
    );
  }
}

class ChoicePage extends StatelessWidget {
  const ChoicePage({Key key, this.choice}) : super(key: key);
  final choice;

  @override
  Widget build(BuildContext context) {
    final TextStyle textStyle = Theme.of(context).textTheme.display1;
    return Card(
      color: Colors.white,
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            
            Text(
              choice.psubCatName,
              style: textStyle,
            ),
          ],
        ),
      ),
    );
  }
}

但是当我使用这个东西时,我的数据就会显示出来。

if (snapshot.hasData) {
 return Column( 
crossAxisAlignment: CrossAxisAlignment.center, 
children: [
 Text(snapshot.data.length.toString()),
 Text(snapshot.data.toString()),
 ],
 );
} 

但是当我使用这个东西时,会在 TabBar 的选项卡内显示错误。 这是错误 ->'Class _InternalLinkedHashMap' 没有实例 getter 'psubCatName'。接收方:_LinkedHashMap 长度:9 尝试调用:psubCatName

                   if (snapshot.hasData) {
                    print(snapshot.data.length.toString());
                    print(snapshot.data.toString());
                    // API data will be stored as snapshot.data
                    return DefaultTabController(
                      length: snapshot.data.length,
                      child: Scaffold(
                        appBar: AppBar(
                          title: const Text('Tabbed AppBar'),
                          bottom: TabBar(
                            isScrollable: true,
                            tabs: snapshot.data
                                .map((choice) => Tab(
                                      text: choice.psubCatName,
                                      // icon: Icon(choice),
                                    ))
                                .toList(),
                          ),
                        ),

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies flutter-test


    【解决方案1】:

    您使用的选择对象是一个地图,而不是访问地图值

    choice.psubCatName
    

    你应该使用:

    choice['psubCatName']
    

    无论如何,将 FutureBuilder 与这样的数据类型一起使用是一个好习惯:

    FutureBuilder<YoureDataType>()
    

    在这种情况下

    FutureBuilder<Map>()
    

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2021-11-20
      • 2021-07-21
      • 2019-01-03
      • 2021-06-29
      • 1970-01-01
      • 2020-01-10
      • 2019-03-23
      • 2021-04-07
      相关资源
      最近更新 更多