尝试直接从 FLUTTER 进行调用:MissingPluginException(未找到方法 callNumber 的实现

Dev*_*esh 2 plugins phone-call dart flutter

我希望我的应用程序在出现某种情况时自动呼叫特定号码。两个流行的插件是 flutter_phone_direct_caller 和 url_launcher。Url 启动器的问题是,该方法会将号码推送到手机的拨号器,但不会启动呼叫,但 flutter_phone_direct_caller 声称它将启动。这是他们文档中的示例。

    import 'package:flutter/material.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';

void main() {
  runApp(Scaffold(
    body: Center(
      child: RaisedButton(
        onPressed: _callNumber,
        child: Text('Call Number'),
      ),
    ),
  ));
}

_callNumber() async{
  const number = '08592119XXXX'; //set the number here
  bool res = await FlutterPhoneDirectCaller.callNumber(number);
}
Run Code Online (Sandbox Code Playgroud)

这是我页面的代码。按下按钮时,应该启动呼叫,但对我来说,它返回一个错误。(我的电话号码是 XXXd,当我运行它时,我输入了我的实际号码)。

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:vitality/components/bottomAppBar.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:vitality/components/biom.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';

class HomeScreen extends StatefulWidget {
  static const String id = 'home_screen';
  final String docid;
  final bool isCaretaker;
  HomeScreen({@required this.docid, @required this.isCaretaker});
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

_callNumber() async {
  const number = '86065XXXXX'; //set the number here
  bool res = await FlutterPhoneDirectCaller.callNumber(number);
}

class _HomeScreenState extends State<HomeScreen> {
  final auth = FirebaseAuth.instance;
  var pulse;
  var temp;
  @override
  Widget build(BuildContext context) {
    print('got here');
    print(auth.currentUser.uid);
    String id = ModalRoute.of(context).settings.arguments;

    CollectionReference main = FirebaseFirestore.instance.collection('maindb');
    main.doc(id).get().then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        print('Document exists on the database');
        pulse = documentSnapshot['pulse'];
        temp = documentSnapshot['temperature'];
      }
    });
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
          backgroundColor: Color(0xFF602247),
          toolbarHeight: 50.0,
          centerTitle: true,
          title: Text(
            'HEALTH TRACKER',
            style: Theme.of(context).textTheme.headline4,
          )),
      body: Container(
        decoration: BoxDecoration(
            image: DecorationImage(
          image:
              NetworkImage('https://cdn.wallpapersafari.com/12/24/GiZRfh.jpg'),
          fit: BoxFit.cover,
          colorFilter: new ColorFilter.mode(
              Colors.black.withOpacity(.7), BlendMode.dstATop),
        )),
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Text(widget.docid),
              Text({widget.isCaretaker}.toString()),
              biom(which: 'pulse', image: 'pulse', docid: widget.docid),
              RoundBorderText(text: 'PULSE'),
              biom(which: 'temperature', image: 'temper', docid: widget.docid),
              RoundBorderText(text: 'TEMPERATURE'),
              SizedBox(height: 30.0),
              FlatButton(
                  child: Text('test call'),
                  onPressed: () async {
                    FlutterPhoneDirectCaller.callNumber('5');
                  })
            ]),
      ),
      bottomNavigationBar: bottomAppBar(id: widget.docid),
    );
  }
}

class RoundBorderText extends StatelessWidget {
  final String text;
  RoundBorderText({this.text});
  @override
  Widget build(BuildContext context) {
    return Container(
        padding: const EdgeInsets.only(
            left: 40.0, right: 40.0, top: 8.0, bottom: 8.0),
        decoration: BoxDecoration(
            // border: Border.all(
            //   color: Colors.black,
            //   width: 1.0,
            // ),
            borderRadius: BorderRadius.all(Radius.circular(20))),
        child: Text(text, style: Theme.of(context).textTheme.headline1));
  }
}
Run Code Online (Sandbox Code Playgroud)

错误

E/flutter(23210):[错误:flutter/lib/ui/ui_dart_state.cc(186)]未处理的异常:MissingPluginException(在通道 flutter_phone_direct_caller 上找不到方法 callNumber 的实现)

这个插件有 94% 的流行度,所以它适用于大多数人。有谁知道问题是什么?

Nis*_*ddy 8

flutter 与本机功能集成的方式是创建所谓的内容,使用它们可以调用在dart 的MethodChannels本机代码中注册的函数。java

因此,出现此错误的一个原因可能是您的 flutter 代码无法与本机 java 代码通信,这意味着它没有找到任何代码channel,或者没有找到method包通过channel那里注册的代码。

我怀疑这可能是构建问题。

脚步

  1. 从您的设备中卸载该应用程序。
  2. 再次重建应用程序。

这应该可以解决问题。