我正在尝试运行我的应用程序,但我不断收到此错误:
错误:类型错误:“玩家”:类型“String”不是类型“int”的子类型 获取比赛历史记录时出错:异常:获取玩家比赛历史记录时出错:TypeError:“players”:类型“String”不是类型“int”的子类型
我正在尝试在 JSON 响应中显示姓名标签和当前已修补的玩家等级。
(它有我的 API 密钥;不要弄乱它)在列表视图图块中,并收到上面的错误。
class Player {
final String name;
final String tag;
final String rank;
Player({required this.name, required this.tag, required this.rank});
factory Player.fromJson(Map<String, dynamic> json) {
return Player(
name: json['name'],
tag: json['tag'],
rank: json['currenttier_patched'], // Use 'rank_patched' instead of 'rank'
);
}
}
class PlayerMatchHistory {
final List<Player> players;
PlayerMatchHistory({required this.players});
factory PlayerMatchHistory.fromJson(Map<String, dynamic> json) {
var data = json['data']; // Get the first element of the 'data' list
var playersJson = data['players']; // Get the 'all_players' field from the 'players' object
if (playersJson is List) {
List<Player> playersList = playersJson.map((playerJson) => Player.fromJson(playerJson)).toList();
return PlayerMatchHistory(players: playersList);
} else {
throw Exception('Expected all_players to be a List but got a ${playersJson.runtimeType}');
}
}
}
用户界面屏幕
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:valorant_app/viewmodel/match_vm.dart';
class PlayerMatchHistoryScreen extends StatefulWidget {
final String playerName;
final String tag;
const PlayerMatchHistoryScreen({
Key? key,
required this.playerName,
required this.tag,
}) : super(key: key);
@override
_PlayerMatchHistoryScreenState createState() => _PlayerMatchHistoryScreenState();
}
class _PlayerMatchHistoryScreenState extends State<PlayerMatchHistoryScreen> {
bool _isListViewVisible = false;
@override
Widget build(BuildContext context) {
final viewModel = Provider.of<PlayerMatchHistoryViewModel>(context);
return Scaffold(
appBar: AppBar(
title: const Text('Player Match History'),
),
body: FutureBuilder(
future: viewModel.fetchMatchHistory(widget.playerName, widget.tag),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (viewModel.isLoading) {
return const Center(child: Text("Loading..."));
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (viewModel.matchHistory == null || viewModel.matchHistory!.players.isEmpty) {
return const Center(child: Text('No match history available.'));
} else {
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
_isListViewVisible = true;
});
});
return _isListViewVisible
? ListView.builder(
itemCount: viewModel.matchHistory!.players.length,
itemBuilder: (context, index) {
final player = viewModel.matchHistory!.players[index];
return ListTile(
title: Text('${player.name}#${player.tag}'),
subtitle: Text('Current Rank: ${player.rank}'),
);
},
)
: Container();
}
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
);
}
}
主要
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:valorant_app/pages/player_screen.dart';
import 'package:valorant_app/service/api.dart';
import 'package:valorant_app/repo/repo.dart';
import 'package:valorant_app/viewmodel/match_vm.dart';
import 'package:valorant_app/viewmodel/valo_vm.dart';
//import 'package:valorant_app/pages/player_screen.dart';
import 'package:valorant_app/pages/player_history.dart'; // Import the new screen
void main() {
// Define base URLs and API key
const baseUrl = 'https://api.henrikdev.xyz/valorant/v2/mmr';
const baseUrl2 = 'https://api.henrikdev.xyz/valorant/v3/matches'; // Correct Base URL
const apiKey = 'HDEV-990890cf-68cf-4263-afce-446dadda0b13'; // Replace with your actual API Key
// Initialize the API service and repository
final apiService = ValorantApiService(baseUrl: baseUrl, baseUrl2: baseUrl2, apiKey: apiKey);
final repository = ValorantRepository(apiService: apiService);
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => PlayerViewModel(repository: repository),
),
ChangeNotifierProvider(
create: (_) => PlayerMatchHistoryViewModel(repository: repository),
),
],
child: const MyApp(),
),
);
}
主要
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Valorant Player Info',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const PlayerMatchHistoryScreen(playerName: 'Gunner', tag: 'GG69', ) // Initial screen can be changed to match history if needed
);
}
}
模型/JSON输出
"players": {
"all_players": [
{
"puuid": "3c4b8e9c-8a7b-5dfc-b68d-4fba11b39291",
"name": "GUNNER",
"tag": "GG69",
"team": "Blue",
"level": 111,
"character": "Breach",
"currenttier": 9,
"currenttier_patched": "Silver 1",
"player_card": "72f9b137-41f1-ef95-853a-798212f064ee",
"player_title": "129190d4-42fa-4e79-75e3-ffb5679d1dc2",
"party_id": "5052b7eb-098e-4943-ac25-08fe747f17cf",
"session_playtime": {
"minutes": 46,
"seconds": 2760,
"milliseconds": 2760000
},
我尝试使用各种人工智能工具来解决这个问题,但没有任何帮助。我是 Flutter 新手,这是我的第一个项目,没有遵循任何教程,所以这就是我能做的。我也尝试在同一问题上搜索一些线程,但没有任何帮助。
我期望的是在列表视图中显示玩家的姓名、标签和当前等级补丁(排名),因为我已将代码共享到上面的 UI 屏幕。
您返回的数据结构略有不同。
该评论可能会分散您的注意力:
var playersJson = data['players']; // Get the 'all_players' field from the 'players' object
你可能想要类似的东西
// Get the 'all_players' array from the 'players' object from the 'data' object
final playersJson = data['players']['all_players'];
这是我们说评论“为什么”而不是“什么”的原因之一。
//Add 1 to x
X+=2;