我正在开发一个AI音乐播放器。但我正面临一个错误,即尝试添加一个初始化器表达式,或者一个初始化它的生成式构造函数,或者将其标记为“late”。在代码Try adding an initializer expression, or a generative constructor that initializes it, or mark it 'late'.的17,18,19行中帮我一下
这是我的代码。我试图添加late关键字,但随后我遇到了casterror null检查运算符对null值的使用。因此,如果可能的话,请附上更正后的代码。
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,
),
);
}
}发布于 2021-11-01 14:07:24
我们不知道你想让这些变量有什么值。
您可以将它们初始化为您选择的值,或者将它们标记为late并在第一次使用它们之前初始化它们,或者您可以使它们可为空(例如,通过将它们声明为Color? _selectedColor; ),然后以一种考虑到在访问它们时可能没有值的方式编写代码。
无论哪种方式,这都是你必须做出的选择。我们不能凭空抽出一个随机值,然后告诉你使用它作为初始值。
https://stackoverflow.com/questions/69797946
复制相似问题