尝试不显示API数据中的特定数字 [英] Trying not to display a specific number from a API data
问题描述
我试图不显示数字零[0],但以某种方式捕获了数字并将其显示在字段中。
并且我认为它将与API结果一起显示,但实际上它是在做
API结果* 3
它打印API结果三次.....
I was trying not to display the number zero[0] but somehow it captured the number and displayed it over in the field. And I thought that it would display up with the API result but what actually it is doing is that API result * 3 It is printing the API result three times.....
import 'package:ample/main.dart';
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> {
var nos;
Ticket ticketList;
String apiResult;
// bool isActive = true;
Map<String, bool> cellStatus = {};
@override
void initState() {
super.initState();
_getNumbers();
}
_getNumbers() async {
var result = await http
.post(
'https://tickets-qzd55332wa-de.a.run.app/generateTickets?ticketsRequired=2')
.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 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}');
//bool isActive = true;
return Scaffold(
body: SafeArea(
child: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: ListView.builder(
itemCount: ticketList.tickets.length,
itemBuilder: (BuildContext context, index) {
List tripleNumbersList = [];
List<Widget> cells = [];
List<Widget> rows = [];
//Get the lenght of the list 'tickets' of the Ticket class
int ticketsCount = ticketList.tickets.length;
//Iterates over the lists inside the 'tickets' list of the Ticket class
for (int i = 0; i < ticketsCount; i++) {
//Get the lists of lists inside the 'tickets' list
tripleNumbersList = ticketList.tickets[i];
//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(Center(
child: Container(
height: 45,
width: 35,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
color: Colors.pink
),
child: GestureDetector(
onTap: () {
if (cellStatus['$i$j$k']?? true) {
print('Working');
setState(() {
cellStatus.addAll({'$i$j$k':false});
});
}
},
child: Text(
' ${list[k]} ',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
),
),
));
}
//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: 30,
)
]));
}
return Center(
child: Container(
height: h,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
//color: Colors.red // covers the big box
),
child: Column(
children: rows,
)
),
);
},
),
),
),
),
);
}
}
}
我不知道我要去哪里错了这样做...
请帮助我实现该算法...多次失败
I do not know where am I going wrong to do so... Please help me with the algorithm to do so...Failed quite a couple of times
推荐答案
要不打印零,您只需要做一个简单的检查,即在GestureDetector内部打印带有数字的Text小部件,就像这样:
To not print the zeros you need just to do a simple check where you are printing the Text widget with the numbers, inside the GestureDetector, like this:
GestureDetector(
onTap: () {
if (cellStatus['$i$j$k']?? true) {
print('Working');
setState(() {
cellStatus.addAll({'$i$j$k':false});
});
}
},
//Check if the value if different of zero
child: list[k] != 0
//If different of zero prints the number
? Text( ' ${list[k]} ', style: TextStyle( fontSize: 18.0, fontWeight: FontWeight.bold), )
//Otherwise, if equal zero, prints an empty text
: Text(''),
)
这篇关于尝试不显示API数据中的特定数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!