How to reload the initstate when ratio is changed Please help me,Have I used set state at proper place ?

34 views
Skip to first unread message

Rishabh Shukla

unread,
May 21, 2020, 8:50:07 AM5/21/20
to Flutter Development (flutter-dev)
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class ChewieListItem extends StatefulWidget {
// This will contain the URL/asset path which we want to play
final VideoPlayerController videoPlayerController;
final bool looping;
final double ratio;

ChewieListItem({
@required this.videoPlayerController,
this.looping,
this.ratio,
Key key,
}) : super(key: key);

@override
_ChewieListItemState createState() => _ChewieListItemState();
}

class _ChewieListItemState extends State<ChewieListItem> {
ChewieController _chewieController;
double ratio;

@override
void initState() {
super.initState();
// Wrapper on top of the videoPlayerController
_chewieController = ChewieController(
videoPlayerController: widget.videoPlayerController,

aspectRatio: ratio,
// Prepare the video to be played and display the first frame
autoInitialize: true,
looping: widget.looping,

// Errors can occur for example when trying to play a video
// from a non-existent URL
errorBuilder: (context, errorMessage) {
return Center(
child: Text(
errorMessage,
style: TextStyle(color: Colors.white),
),
);
},
);
setState(() {
ratio = widget.ratio;
});
}

@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8.0),
child: Chewie(
controller: _chewieController,
),
);
}

@override
void dispose() {
super.dispose();
// IMPORTANT to dispose of all the used resources

widget.videoPlayerController.dispose();
_chewieController.dispose();
}
}

Souvik Dutta

unread,
May 21, 2020, 11:33:13 AM5/21/20
to Rishabh Shukla, Flutter Development (flutter-dev)
Probably yes you have done a mistake in placing set state in your code. Can you say more about how you intend to use set state like when you want to rebuild your widget?  

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/41a39f53-e31b-42ca-b999-c88090891581%40googlegroups.com.

Danilo Costa Viana

unread,
May 21, 2020, 11:37:05 AM5/21/20
to Flutter Development (flutter-dev)

"initState" is already a method intended to set the state the first time state class is instantiated, so there is no need to assign ratio = widget.ration inside a setState statement (and you're probably getting an error).

Also when you're declaring _chewieController = ChewieController the ratio is still null (Dart doesn't have primitive types, int and double are objects and their default value is null, not 0).

It seems what you want is to update your state whenever ChewieListItem is rebuilt with a new ratio passed as argument. In that case you need to override didChangeDependencies and inside that you can reassign ratio=widget.ratio (again, no need for setState because didChangeDependencies runs before build) and maybe update your controller accordingly, as it seems to need the ratio.

didChangeDependencies is called every time your widget is rebuilt with a new configuration (new arguments, like a new ratio), it's the Flutter way of saying "hey state, your parent widget changed, so maybe you want to get it's arguments again to update yourself". initState is only called when the state is first created while didChangeDependencies is called if the state is maintained but widget that owns the state changed. didChangeDependencies is also called after initState the first time so you probably can just move your code there.
Reply all
Reply to author
Forward
0 new messages