gpt4 book ai didi

arrays - 在API中使用用户输入来按用户加载数据时发现异常

转载 作者:行者123 更新时间:2023-12-03 04:42:31 24 4
gpt4 key购买 nike

我试图使用用户输入从ListView Builder中的API接收数据,以显示要显示的票证数量。
这是我试图反射(reflect)用户输入的地方。
每次我在列表 View 生成器之前使用文本字段时,我都收到错误

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

class Ticket {
Ticket({
this.tickets,
});

List<List<List<int>>> tickets;

factory Ticket.fromJson(Map<String, dynamic> json) => Ticket(
tickets: List<List<List<int>>>.from(json["tickets"].map((x) =>
List<List<int>>.from(
x.map((x) => List<int>.from(x.map((x) => x)))))),
);

Map<String, dynamic> toJson() => {
"tickets": List<dynamic>.from(tickets.map((x) => List<dynamic>.from(
x.map((x) => List<dynamic>.from(x.map((x) => x)))))),
};
}

class TicketPage extends StatefulWidget {
@override
_TicketPageState createState() => _TicketPageState();
}

class _TicketPageState extends State<TicketPage> {
String _nos = '0';
Ticket ticketList;
String apiResult;
Map<String, bool> cellStatus = {};
final numCon = TextEditingController();

@override
void initState() {
super.initState();
_getNumbers();
numCon.addListener(_updateUserInput);
}

_updateUserInput() {
_nos = numCon.text;
}

@override
void dispose() {
// Clean up the controller when the widget is removed from the widget tree.
// This also removes the listener.
numCon.dispose();
super.dispose();
}

_getNumbers() async {
var result = await http
.post(
'https://tickets-qzd55332wa-de.a.run.app/generateTickets?ticketsRequired=$_nos')
.then((result) {
//Waits for the API response and assigns to apiResult variable
setState(() {
apiResult = result.body;
});
});
}

// List tick = [
// {
// 'tickets': [
// [
// [11, 5, 7, 10, 28, 9, 7, 74, 59],
// [1, 15, 7, 10, 8, 79, 27, 74, 9],
// [71, 5, 7, 20, 18, 9, 77, 74, 79],
// ],
// [
// [21, 5, 7, 80, 8, 9, 7, 74, 49],
// [31, 15, 7, 10, 18, 79, 7, 74, 19],
// [71, 5, 7, 20, 18, 79, 77, 74, 29],
// ],
// ]
// },
// ];

@override
Widget build(BuildContext context) {
var h = MediaQuery.of(context).size.height;
var w = MediaQuery.of(context).size.width;

if (apiResult == null) {
return Scaffold(body: Center(child: CircularProgressIndicator()));
} else {
//Get an instance of Ticket from the API assigned to apiResponse variable
ticketList = Ticket.fromJson(json.decode(apiResult));
print('Tickets: ${ticketList.tickets}');

return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(28.0),
child: Container(
child: TextField(
controller: numCon,
decoration: InputDecoration(
hintText: 'nos',
),
),
),
),
RaisedButton(onPressed: () {
setState(() {
_getNumbers();
});
}),
Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
)),
child: ListView.builder(
itemCount: ticketList.tickets.length,
itemBuilder: (BuildContext context, index) {
List tripleNumbersList = [];
List<Widget> cells = [];
List<Widget> rows = [];

//Get the lists of lists inside the 'tickets' list
tripleNumbersList = ticketList.tickets[index];
//Iterates over each list with other 3 lists
for (int j = 0; j < tripleNumbersList.length; j++) {
//Get one of the 3 lists
List<int> list = tripleNumbersList[j];
//Iterates over the list of numbers
for (int k = 0; k < list.length; k++) {
//Adds a Widget to 'cells; list for each number
cells.add(Container(
height: 40,
width: 40,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
//color: Colors.pink
),
child: GestureDetector(
onTap: () {
print('Working');
if (cellStatus['$j$k'] ?? true) {
print('Working');
setState(() {
cellStatus.addAll({'$j$k': false});
});
}
},
child: list[k] != 0
? Text(
' ${list[k]} ',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold),
)
: Text(''),
)));
}
//Adds the list of 'cells' in the 'rows' list
rows.add(Row(children: cells));
cells = [];
}
//Adds a empty row to make space
rows.add(Row(children: [
Container(
height: 10,
)
]));

return Center(
child: Container(
height: h / 5,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
//color: Colors.pink
),
child: Column(
//Adds the list of rows to the column
children: rows,
),
),
);
},
),
),
),
),
],
),
),
);
}
}
}
而且它抛出异常
RenderBox was not laid out: RenderCustomPaint#aecb2 relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1694 pos 12: 'hasSize'
The relevant error-causing widget was
ListView
RenderBox was not laid out: RenderPadding#d9b88 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1694 pos 12: 'hasSize'
The relevant error-causing widget was
Scaffold
我希望我可以解释该问题,并希望能帮助我解决该问题。
我从很多方面就陷入了这个问题。

最佳答案

由于ListView嵌套在Column中而发生错误。
可以修改以上代码,将ListView包装在Expanded中。关于this answer
范例:

  @override
Widget build(BuildContext context) {
return _buildList(context);
}

Widget _buildList(BuildContext context) {
var h = MediaQuery.of(context).size.height;

if (apiResult == null) {
return Scaffold(body: Center(child: CircularProgressIndicator()));
} else {
//Get an instance of Ticket from the API assigned to apiResponse variable
ticketList = Ticket.fromJson(json.decode(apiResult));
print('${ticketList.tickets.length} Tickets: ${ticketList.tickets}');

return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('SO 62967820'),
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(28.0),
child: Container(
child: TextField(
controller: numCon,
decoration: InputDecoration(
hintText: 'nos',
),
),
),
),
RaisedButton(
child: Text('Generate'),
onPressed: () {
setState(() {
_getNumbers();
});
}),
Expanded(
child: ListView.builder(
itemCount: ticketList.tickets.length,
itemBuilder: (BuildContext context, index) {
List tripleNumbersList = [];
List<Widget> cells = [];
List<Widget> rows = [];

//Get the lists of lists inside the 'tickets' list
tripleNumbersList = ticketList.tickets[index];
//Iterates over each list with other 3 lists
for (int j = 0; j < tripleNumbersList.length; j++) {
//Get one of the 3 lists
List<int> list = tripleNumbersList[j];
//Iterates over the list of numbers
for (int k = 0; k < list.length; k++) {
//Adds a Widget to 'cells; list for each number
cells.add(Container(
height: 40,
width: 40,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
//color: Colors.pink
),
child: GestureDetector(
onTap: () {
print('Working');
if (cellStatus['$j$k'] ?? true) {
print('Working');
setState(() {
cellStatus.addAll({'$j$k': false});
});
}
},
child: list[k] != 0
? Text(
' ${list[k]} ',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold),
)
: Text(''),
)));
}
//Adds the list of 'cells' in the 'rows' list
rows.add(Row(children: cells));
cells = [];
}
//Adds a empty row to make space
rows.add(Row(children: [
Container(
height: 10,
)
]));

return Center(
child: Container(
height: h / 5,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
//color: Colors.pink
),
child: Column(
//Adds the list of rows to the column
children: rows,
),
),
);
},
),
),
],
),
),
);
}
}

关于arrays - 在API中使用用户输入来按用户加载数据时发现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62952783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com