flutter 带CUBIT的URL启动器

bqujaahr  于 2023-02-20  发布在  Flutter
关注(0)|答案(1)|浏览(150)

我是新的Flutter和块肘。试图学习肘和使用url_launcher包在我的应用程序。我正在使用mealdb的API和去youtube视频与它。我发现youtube链接内的API创建的存储库和一切只是不能得到启动youtube端。我看不到任何在调试控制台也。检查yaml文件url_launcher包它是最新的。
这是我的屏幕侧:

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(),)
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),

肘。省道边:

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';
      
    }
  }
  
}

但我按下按钮时什么也没发生。

y1aodyip

y1aodyip1#

我找到了解决办法: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';
      
    }
  }

视图侧:

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(),
                                                )
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),

相关问题