Reputation: 21
I'm new to flutter and bloc cubit.Trying to learn cubit and use url_launcher package in my app. I'm using mealdb's API and go to youtube video with it. I found youtube link inside the API created repository and everything just cant get to launch youtube side. I cant see anything in debug console either. Checked the yaml file to url_launcher package its up to date.
This my screen side:
GestureDetector(
onTap: () {
context.read<FoodDetailsCubit>().urlLauncher; }, child: Container(height: 6.h, width: 65.w,
margin: EdgeInsets.only(right: 2.w),
decoration: youtubeBoxDecoration(),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [ Icon(Icons.smart_display_outlined, size: 5.5.h,
color: AppColors.white,),
SizedBox(width: 1.w,),
Text("Watch the Tutorial Video",style: Styles.normalWhiteBoldFontStyle(),)
],
),
),
),
),
cubit.dart side:
class FoodDetailsCubit extends Cubit<FoodDetailsState> {
FoodDetailsCubit() : super(FoodDetailsState()) {
loadFoodDetails(52772);
}
final FoodDetailsService api = FoodDetailsService();
Future<void> loadFoodDetails(int foodId) async {
try {
emit(state.copyWith(
foodDetailsStatus: ApiRequest.requestInProgress,
));
final response = (await api.getResFoodDetailsWithId(foodId))?.data;
print(response);
if (response?.meals != null) {
emit(state.copyWith(
foodDetailsStatus: ApiRequest.requestSuccess,
foodDetails: response,
));
} else {
emit(state.copyWith(
foodDetailsStatus: ApiRequest.requestFailure,
));
}
} catch (e) {
emit(state.copyWith(
foodDetailsStatus: ApiRequest.requestFailure,
));
}
}
Future<void> urlLauncher(Uri url) async{
final Uri url = Uri.parse('${state.foodDetails?.meals?[0].strYoutube}');
if (await launchUrl(url)) {
throw 'Could not launch video';
}
}
}
But nothing happens when i press tthe button.
Upvotes: 0
Views: 128
Reputation: 21
I found the solution: cubit.dart:
Future<void> urlLauncher(Uri url) async{
final Uri url = Uri.parse('${state.foodDetails?.meals?[0].strYoutube}');
final Uri url2 =
Uri.parse("https://www.youtube.com/watch?v=4aZr5hZXP_s");
if (await launchUrl(url)) {
throw 'Could not launch video';
}
}
view side:
GestureDetector(
onTap: () {
context
.read<FoodDetailsCubit>()
.urlLauncher(Uri());
},
child: Container(
height: 6.h,
width: 65.w,
margin: EdgeInsets.only(right: 2.w),
decoration: youtubeBoxDecoration(),
child: Center(
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
Icon(
Icons.smart_display_outlined,
size: 5.5.h,
color: AppColors.white,
),
SizedBox(
width: 1.w,
),
Text(
"Watch the Tutorial Video",
style: Styles
.normalWhiteBoldFontStyle(),
)
],
),
),
),
),
Upvotes: 0