[英]How to solve the getter 'value' was called on null in flutter
I am using the video_player
package.我正在使用video_player
包。 I want to load the video.我想加载视频。 The video data is the response from API.视频数据是来自 API 的响应。 But I have error about NoSuchMethodError: The getter 'value' was called on null. Receiver: null Tried calling: value
但是我有关于NoSuchMethodError: The getter 'value' was called on null. Receiver: null Tried calling: value
错误NoSuchMethodError: The getter 'value' was called on null. Receiver: null Tried calling: value
NoSuchMethodError: The getter 'value' was called on null. Receiver: null Tried calling: value
. NoSuchMethodError: The getter 'value' was called on null. Receiver: null Tried calling: value
。 My response is no problem.我的回答没有问题。 I print()
the response there got videoURL.我print()
那里的响应得到了 videoURL。 The problem is I don't know how to add the video_player
after get the response.问题是我不知道如何在得到响应后添加video_player
。 I have tried to do like this but have error.我试过这样做,但有错误。
Here is my code:这是我的代码:
class _State extends State<MyApps> {
GlobalKey<PaginatorState> paginatorGlobalKey = GlobalKey();
String videoURL;
VideoPlayerController _videoPlayerController;
@override
void initState() {
super.initState();
playVideo(videoURL);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('My Apps'),
),
body: Paginator.listView(
key: paginatorGlobalKey,
pageLoadFuture: sendPagesDataRequest,
pageItemsGetter: listItemsGetterPages,
listItemBuilder: listItemBuilder,
loadingWidgetBuilder: loadingWidgetMaker,
errorWidgetBuilder: errorWidgetMaker,
emptyListWidgetBuilder: emptyListWidgetMaker,
totalItemsGetter: totalPagesGetter,
pageErrorChecker: pageErrorChecker,
scrollPhysics: BouncingScrollPhysics(),
),
);
}
Future<Data> sendPagesDataRequest(int page) async {
try {
String url = Uri.encodeFull("APIURL?page=$page");
http.Response response = await http.get(url);
Data pagesData = pagesDataFromJson(response.body);
return pagesData;
} catch (e) {
}
void playVideo(dynamic item) async {
_videoPlayerController = VideoPlayerController.network(item.videoURL)
..initialize().then((_) {
_videoPlayerController.play();
});
}
Widget listItemBuilder(dynamic item, int index) {
videoURL= item.videoURL;
return InkWell(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
elevation: 4,
margin: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Stack(
children: <Widget>[
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
_videoPlayerController.value.initialized
? AspectRatio(
aspectRatio: _videoPlayerController.value.aspectRatio,
child: VideoPlayer(_videoPlayerController),
)
: Container()
],
),
),
],
),
),
);
}
What is the error in my code and how can I solve it?我的代码中有什么错误,我该如何解决?
You should store the Future returned from the VideoPlayerController.initialize
method in a State Class Variable:您应该将VideoPlayerController.initialize
方法返回的 Future 存储在状态类变量中:
Future hasInitialized;
void playVideo(dynamic item) async {
_videoPlayerController = VideoPlayerController.network(item.videoURL);
hasInitialized = initialize();
hasInitialized.then((_) {
_videoPlayerController.play();
});
}
Then, you can use a FutureBuilder class to build a widget depending if the Future has been completed or not.然后,您可以使用FutureBuilder 类来构建一个小部件,具体取决于Future 是否已完成。
The problem is I don't know how to add the video_player after get the response.问题是我不知道如何在得到响应后添加 video_player。
This Widget can return different things wether or not a Future has completed by using the connectionState
or the snapshot.hasData
(in case of non-void Futures) attributes.这个 Widget 可以通过使用connectionState
或snapshot.hasData
(在非空的 Futures 的情况下)属性来返回不同的东西,无论 Future 是否完成。
FutureBuilder(
future: hasInitialized,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// Return here what you want to display.
} else {
// Return here what you want to show when the VideoPlayerController is still initializing, like a
// loading spinner.
return Center(child: CircularProgressIndicator());
}
},
)
For more information see the official Cookbook.有关更多信息,请参阅官方食谱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.