flutter error: _InternalLinkedHashMap<String, dynamic>
flutter error: _InternalLinkedHashMap<String, dynamic>
错误-> Class '_InternalLinkedHashMap' 没有实例 getter 'psubCatName'。
接收方:_LinkedHashMap len: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,
),
],
),
),
);
}
}
但是当我使用 This thing 时,我的数据显示。
if (snapshot.hasData) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(snapshot.data.length.toString()),
Text(snapshot.data.toString()),
],
);
}
但是当我使用 This thing 时,TabBar 的 Inside 选项卡显示错误。
这是错误 ->'Class _InternalLinkedHashMap<String, dynamic>' 没有实例 getter 'psubCatName'。接收方:_LinkedHashMap len: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(),
),
),
您使用的选择对象是地图并且访问地图值而不是
choice.psubCatName
你应该使用:
choice['psubCatName']
无论如何,使用具有如下数据类型的 FutureBuilder 是一个好习惯:
FutureBuilder<YoureDataType>()
在这种情况下
FutureBuilder<Map>()
错误-> Class '_InternalLinkedHashMap
这是我的 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,
),
],
),
),
);
}
}
但是当我使用 This thing 时,我的数据显示。
if (snapshot.hasData) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(snapshot.data.length.toString()),
Text(snapshot.data.toString()),
],
);
}
但是当我使用 This thing 时,TabBar 的 Inside 选项卡显示错误。 这是错误 ->'Class _InternalLinkedHashMap<String, dynamic>' 没有实例 getter 'psubCatName'。接收方:_LinkedHashMap len: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(),
),
),
您使用的选择对象是地图并且访问地图值而不是
choice.psubCatName
你应该使用:
choice['psubCatName']
无论如何,使用具有如下数据类型的 FutureBuilder 是一个好习惯:
FutureBuilder<YoureDataType>()
在这种情况下
FutureBuilder<Map>()