Flutter赛事比分预测匹配逻辑异常问题求助
问题描述
我需要实现以下功能:当赛事最终比分与预测比分一致时,列表项尾部显示绿色文本“Won”;若不一致,则显示红色文本“Lost”。但目前逻辑存在错误,无论实际结果如何,尾部始终显示“Lost”。
MyListTile 组件代码
import 'package:flutter/material.dart'; class MyListTile extends StatelessWidget { const MyListTile({ Key? key, required this.homeTeamName, required this.awayTeamName, required this.scoreFulltime, required this.oddsString, }) : super(key: key); final String homeTeamName; final String awayTeamName; final String scoreFulltime; final String oddsString; @override Widget build(BuildContext context) { final predictedScore = oddsString.split(' ')[0]; final hasWon = scoreFulltime == predictedScore; return ListTile( leading: CircleAvatar( child: Text(homeTeamName[0]), ), contentPadding: const EdgeInsets.symmetric(horizontal: 30.0, vertical: 20.0), title: Padding( padding: const EdgeInsets.only(bottom: 10.0), child: Text( '$homeTeamName vs $awayTeamName', style: const TextStyle(fontWeight: FontWeight.w500, fontSize: 22), ), ), subtitle: RichText( text: TextSpan( text: 'Score:', style: const TextStyle( fontSize: 20, fontWeight: FontWeight.w800, color: Colors.blueAccent, ), children: <TextSpan>[ TextSpan( text: ' $scoreFulltime ', style: const TextStyle(color: Colors.black54), ), const TextSpan( text: ' Prediction:', style: TextStyle(color: Colors.blueAccent), ), TextSpan( text: ' $oddsString', style: const TextStyle(color: Colors.black54), ), ], ), ), trailing: Container( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4), decoration: BoxDecoration( borderRadius: BorderRadius.circular(5), color: hasWon ? Colors.green : Colors.red, ), child: Text( hasWon ? 'Won' : 'Lost', style: const TextStyle( color: Colors.white, fontWeight: FontWeight.bold, ), ), ), ); } }
ListView 构建代码
import 'package:flutter/material.dart'; import '../tips_widget.dart'; ListView myListBuilder(List<Map<String, dynamic>> tips) { return ListView.builder( itemCount: tips.length, itemBuilder: (context, index) { final tip = tips[index]; final homeTeamName = tip['team1']; final awayTeamName = tip['team2']; final scoreFulltime = tip['score_fulltime']; final bettingTips = tip['betting_tips']; final odds = <String>[]; bettingTips.forEach((key, value) { odds.add(value['odds']); }); final oddsString = odds.join(' / '); return MyListTile( homeTeamName: homeTeamName, awayTeamName: awayTeamName, scoreFulltime: scoreFulltime, oddsString: oddsString, ); }, ); }
问题原因分析
- 分割符不匹配:在
MyListTile中用空格分割oddsString获取预测比分,但oddsString是通过/拼接生成的,用空格分割无法得到正确的预测比分,导致对比逻辑永远不成立。 - 字段可能混淆:如果
bettingTips里的odds字段是赔率而非预测比分,那整个对比逻辑的数据源就错了,需要确认预测比分对应的字段名。
修复方案
方案1:修正分割逻辑(假设odds字段就是预测比分)
在MyListTile中替换预测比分的获取方式,适配/的分隔符,并处理可能的空格:
// 替换原有的predictedScore和hasWon逻辑 final predictedScore = oddsString.split(' / ')[0].trim(); final hasWon = scoreFulltime.trim() == predictedScore;
方案2:传递正确的预测比分字段(如果odds是赔率)
若bettingTips中有单独的预测比分字段(比如predicted_score),直接传递该字段给MyListTile:
- 修改
MyListTile的参数定义:
class MyListTile extends StatelessWidget { const MyListTile({ Key? key, required this.homeTeamName, required this.awayTeamName, required this.scoreFulltime, required this.predictedScore, // 新增预测比分参数 required this.oddsString, }) : super(key: key); final String homeTeamName; final String awayTeamName; final String scoreFulltime; final String predictedScore; // 新增字段 final String oddsString; @override Widget build(BuildContext context) { final hasWon = scoreFulltime.trim() == predictedScore.trim(); // 其余代码保持不变 } }
- 在ListView构建时传递正确的字段:
// 在myListBuilder中修改 final odds = <String>[]; String predictedScore = ''; bettingTips.forEach((key, value) { odds.add(value['odds']); predictedScore = value['predicted_score']; // 替换为实际的预测比分字段名 }); final oddsString = odds.join(' / '); return MyListTile( homeTeamName: homeTeamName, awayTeamName: awayTeamName, scoreFulltime: scoreFulltime, predictedScore: predictedScore, oddsString: oddsString, );
内容的提问来源于stack exchange,提问作者Jesse Josiah
相关产品推荐
相关产品推荐

