【发布时间】:2021-08-29 18:24:29
【问题描述】:
Error-> 类 '_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,
),
],
),
),
);
}
}
但是当我使用这个东西时,我的数据就会显示出来。
if (snapshot.hasData) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(snapshot.data.length.toString()),
Text(snapshot.data.toString()),
],
);
}
但是当我使用这个东西时,会在 TabBar 的选项卡内显示错误。
这是错误 ->'Class _InternalLinkedHashMap
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