我正面臨 _CastError 并且無法修復它。如果有人可以更正現有代碼并附上它,我們將不勝感激。我已經使用了!運營商,我認為這是這個問題的主要原因。我不知道如何處理這個問題。更正的代碼和附件將不勝感激這是我的代碼:我還附上了錯誤截圖,所以請看一看。
_CastError 截圖
import 'package:ai_music_player/model/radio.dart';
import 'package:ai_music_player/utils/ai_utils.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:velocity_x/velocity_x.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<MyRadio>? radios;
MyRadio? _selectedRadio;
Color? _selectedColor;
bool _isPlaying = false;
final AudioPlayer _audioPlayer = AudioPlayer();
@override
void initState() {
super.initState();
fetchRadios();
_audioPlayer.onPlayerStateChanged.listen((event) {
if (event == PlayerState.PLAYING) {
_isPlaying = true;
} else {
_isPlaying = false;
}
setState(() {});
});
}
fetchRadios() async {
final radioJson = await rootBundle.loadString("assets/radio.json");
radios = MyRadioList.fromJson(radioJson).radios;
setState(() {});
}
playMusic(String url) {
_audioPlayer.play(url);
_selectedRadio = radios!.firstWhere((element) => element.url == url);
print(_selectedRadio!.name);
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(),
body: Stack(
children: [
VxAnimatedBox()
.size(context.screenWidth, context.screenHeight)
.withGradient(
LinearGradient(
colors: [AIColors.primaryColor1, AIColors.primaryColor2],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
)
.make(),
AppBar(
title: "AI Player".text.xl4.bold.white.make().shimmer(
primaryColor: Vx.purple300, secondaryColor: Colors.white),
backgroundColor: Colors.transparent,
elevation: 0.0,
centerTitle: true,
).h(120),
radios != null
? VxSwiper.builder(
itemCount: radios!.length,
enlargeCenterPage: true,
itemBuilder: (context, index) {
final rad = radios![index];
return VxBox(
child: ZStack(
[
Positioned(
top: 0,
right: 0,
child: VxBox(
child: rad.lang.text.uppercase.bold.white
.make()
.px16(),
)
.height(40)
.black
.alignCenter
.withRounded(value: 10)
.make(),
),
Align(
alignment: Alignment.bottomCenter,
child: VStack(
[
rad.name.text.xl5.white.bold.make(),
5.heightBox,
rad.tagline.text.xl.white.bold.italic.make(),
],
crossAlignment: CrossAxisAlignment.center,
),
),
Align(
alignment: Alignment.center,
child: [
const Icon(
CupertinoIcons.play_circle,
color: Colors.white54,
size: 60,
),
10.heightBox,
"DOUBLE TAB TO PLAY".text.gray300.make()
].vStack(),
),
],
),
)
.clip(Clip.antiAlias)
.bgImage(
DecorationImage(
image: NetworkImage(rad.image),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.3),
BlendMode.darken),
),
)
.border(color: Colors.black, width: 3)
.withRounded(value: 60.0)
.make()
.onInkDoubleTap(() {
_audioPlayer.play(rad.url);
});
},
aspectRatio: 1.0,
).centered()
: const Center(
child: CircularProgressIndicator(
color: Colors.white,
),
),
Align(
alignment: Alignment.bottomCenter,
child: [
if (_isPlaying)
"Playing - ${_selectedRadio!.name} FM".text.makeCentered(),
Icon(
_isPlaying
? CupertinoIcons.stop_circle
: CupertinoIcons.play_circle,
color: Colors.white,
size: 60,
).onInkTap(() {
if (_isPlaying) {
_audioPlayer.stop();
} else {
_audioPlayer.play(_selectedRadio!.url);
}
})
].vStack(),
).pOnly(bottom: context.percentHeight * 12)
],
fit: StackFit.expand,
),
);
}
}
uj5u.com熱心網友回復:
你想讓我給你更正后的代碼,不幸的是,這不是那么簡單,我不知道如何修復代碼,因為我不知道你想要發生什么,但是我可以給你建議,我可以解釋這些建議的利弊:
_audioPlayer.play(_selectedRadio!.url);
在這一行中出現錯誤這一事實意味著它_selectedRadio為空,這意味著這url不是一回事。
閱讀您的代碼后,我注意到您只_selectedRadio在 eplayMusic方法上賦值,但我沒有看到您呼叫該方法,因此您有三個選擇:
首先,您可以為 分配一個默認值以_selectedRadio使其永遠不會成為null
從:
MyRadio? _selectedRadio;
到:
MyRadio _selectedRadio = MyRadio(); // I don't know how you initialize this
第二:您可以檢查是否_selectedRadio為`null
從:
} else {
_audioPlayer.play(_selectedRadio!.url);
}
到;
} else {
if (_selectedRadio != null) {
_audioPlayer.play(_selectedRadio!.url);
}
}
最后,如果_selectedRadio是,您可以提供一些后備值來播放null
從:
_audioPlayer.play(_selectedRadio!.url);
到:
_audioPlayer.play(_selectedRadio?.url ?? 'some other url');
您的決定取決于您希望應用程式做什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/344847.html
上一篇:Flutter使用“.withOpacity”導致“該欄位被初始化為一個非常數值”
下一篇:我的顫振網路構建失敗了
