功能的输出与预期结果不对应



我想添加一个功能,如果比赛的最终比分等于预测比分,那么后面的文本应该是"颜色是绿色的,否则应该是"lost"。红色的。但是我没有得到正确的逻辑,无论结果如何,后面的文本显示丢失。

这是下面的代码。

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,
),
),
),
);
}
}

分数屏幕截图

这是MyListTile的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,
);
},
);
}

我想你应该试试这个:

final hasWon = scoreFulltime == oddsString.replaceAll(“-”,”:”);