无法通过Flutter应用打开浏览器URL

时间:2019-04-22 12:23:24

标签: dart flutter unhandled-exception flutter-dependencies

我正在制作一个新闻应用程序,其中显示新闻列表以及其源网站的标题,描述和URL。 这是我的newsDetail.dart页面

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:url_launcher/url_launcher.dart';

import '../scoped_models/main.dart';
import '../models/news.dart';

class NewsDetailPage extends StatelessWidget {

  _launchURL(String url) async {
    String url1 = url;
    if (await canLaunch(url1)) {
      await launch(url1);
    } else {
      throw 'Could not launch $url1';
    }
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ScopedModelDescendant<MainModel>(
      builder: (BuildContext context, Widget child, MainModel model) {
        return WillPopScope(
          onWillPop: () {
            print('back button presses');
            Navigator.pop(context, false);
            return Future.value(false);
          },
          child: ScopedModelDescendant<MainModel>(
              builder: (BuildContext context, Widget child, MainModel model) {
            final News news = model.allnews[model.selectedIndex];
            print('news details main');
            print(news);
            return Scaffold(
              appBar: AppBar(
                title: Text(news.title),
              ),
              body: Column(
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.all(20.0),
                    child: Text(
                      news.description,
                      style: TextStyle(
                        fontSize: 20.0,
                      ),
                    ),
                  ),
                  Center(
                    child: FlatButton(
                      onPressed: (){
                        _launchURL(news.url);
                      },
                      child: Text('further details'),
                    ),
                  ),
                ],
              ),
            );
          }),
        );
      },
    );
  }
}

news.url是一个字符串,其中包含选定新闻的URL。我已经通过_launchURL方法传递了url。 (此方法是此链接的确切副本:https://pub.dartlang.org/packages/url_launcher#-readme-tab-

但是当我单击那个FlatButton时,它给我一个错误:

[ERROR:flutter/shell/common/shell.cc(186)] Dart Error: Unhandled exception:
E/flutter ( 5959): MissingPluginException(No implementation found for method canLaunch on channel plugins.flutter.io/url_launcher)
E/flutter ( 5959): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:291:7)
E/flutter ( 5959): <asynchronous suspension>
E/flutter ( 5959): #1      canLaunch (package:url_launcher/url_launcher.dart:111:25)
E/flutter ( 5959): <asynchronous suspension>
E/flutter ( 5959): #2      NewsDetailPage._launchURL (package:project/widgets/newsdetail.dart:12:15)
E/flutter ( 5959): <asynchronous suspension>
E/flutter ( 5959): #3      NewsDetailPage.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:project/widgets/newsdetail.dart:52:25)
E/flutter ( 5959): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
E/flutter ( 5959): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
E/flutter ( 5959): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter ( 5959): #7      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter ( 5959): #8      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
E/flutter ( 5959): #9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter ( 5959): #10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter ( 5959): #11     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter ( 5959): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:180:19)
E/flutter ( 5959): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22)
E/flutter ( 5959): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7)
E/flutter ( 5959): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
E/flutter ( 5959): #16     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
E/flutter ( 5959): #17     _invoke1 (dart:ui/hooks.dart:173:13)
E/flutter ( 5959): #18     _dispatchPointerDataPacket (dart:ui/hooks.dart:127:5)

我有url_launcher:^ 5.0.2 已经安装在py pubspec.yaml文件中。请告诉您错误是什么以及解决方法。

3 个答案:

答案 0 :(得分:2)

尝试重新启动应用程序。 不是热重启或热重载;完全重启

答案 1 :(得分:0)

尝试在不使用“ await”的情况下使用函数launch。() 希望它能工作 只是:launch()

答案 2 :(得分:0)

我删除了该应用,然后重新安装,在android中工作

if($shouldProcess){
    $photo = new \InstagramAPI\Media\Photo\InstagramPhoto($target_file);
    $uploadResult = $insta->timeline->uploadPhoto($photo->getFile(), ['caption' => $caption]);
}else{
    $uploadResult = $insta->timeline->uploadPhoto($target_file, ['caption' => $caption]);
}