android - FlutterActivity MethodChannel和FlutterView

举报
坚果的博客 发表于 2021/12/15 23:17:07 2021/12/15
【摘要】 android - FlutterActivity MethodChannel和FlutterView 因此,大约4个月前,我编写了Flutter应用程序。现在,我想做一个小小的更改,但是我不能再编译应...

android - FlutterActivity MethodChannel和FlutterView

因此,大约4个月前,我编写了Flutter应用程序。现在,我想做一个小小的更改,但是我不能再编译应用程序了,因为GeneratedPluginRegistrant.registerWith(this)不再起作用了,我没有更改Kotlin代码,只更改了Dart代码。

“GeneratedPluginRegistrant.registerWith( this )”中的“”向我显示此错误:

Type mismatch.    
Required: FlutterEngine!    
Found: MainActivity
  
 
  • 1
  • 2

MainActivity类:

import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.view.FlutterMain

class MainActivity : FlutterActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    GeneratedPluginRegistrant.registerWith(this) // here is the error: Type mismatch. Required: FlutterEngine! Found: MainActivity

    MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
      if (call.method == "helloFromNativeCode") {
        val greetings = helloFromNativeCode()
        result.success(greetings)
      }
    }
  }

  private fun helloFromNativeCode(): String {
    return "Hello from Native Android Code"
  }

  companion object {
    private const val CHANNEL = "flutter.native/helper"
  }
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

如果使用:

import io.flutter.embedding.android.FlutterActivity
  
 

    代替

    import io.flutter.app.FlutterActivity
      
     

      我可以用

      override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
              GeneratedPluginRegistrant.registerWith(flutterEngine);
          }
        
       
      • 1
      • 2

      但遇到以下问题:

      MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "helloFromNativeCode") {
              val greetings = helloFromNativeCode()
              result.success(greetings)
            }
          }
        
       
      • 1
      • 2
      • 3
      • 4
      • 5

      因为我在 flutterView 上遇到错误:

      Unresolved reference: flutterView
        
       

        代码如下所示:

        import android.os.Bundle
        import io.flutter.embedding.android.FlutterActivity
        import io.flutter.embedding.engine.FlutterEngine
        import io.flutter.plugin.common.MethodChannel
        import io.flutter.plugins.GeneratedPluginRegistrant
        import io.flutter.view.FlutterMain
        
        class MainActivity : FlutterActivity() {
            override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
                GeneratedPluginRegistrant.registerWith(flutterEngine);
            }
        
          override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
        
            MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result -> // here is the error
              if (call.method == "helloFromNativeCode") {
                val greetings = helloFromNativeCode()
                result.success(greetings)
              }
            }
          }
        
          private fun helloFromNativeCode(): String {
            return "Hello from Native Android Code"
          }
        
          companion object {
            private const val CHANNEL = "flutter.native/helper"
          }
        }
          
         
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30

        我希望有一个人可以帮助我。

        最佳答案

        代替flutterView,请使用flutterEngine.getDartExecutor()。

        关于android - FlutterActivity MethodChannel和FlutterView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59323505/

        import 'package:flutter/material.dart';
        import 'package:flutter/services.dart';
        import 'package:flutter_package_demo/theme/themes.dart';
        
        class UserPage extends StatefulWidget {
          const UserPage({Key key}) : super(key: key);
        
          @override
          _UserPageState createState() => _UserPageState();
        }
        
        const String CHANNEL =
            "www.luckly.work"; //这儿要与        MethodChannel(flutterView, CHANNEL)中CHANNEL名称一致
        const String invokeMethod =
            "breeze"; //这儿要与       call.method == invokeMethod中invokeMethod名称一致
        var channel = const MethodChannel(CHANNEL);
        
        class _UserPageState extends State<UserPage> {
          String _result = "";
        
          @override
          Widget build(BuildContext context) {
            final theme = Theme.of(context);
            return Scaffold(
              appBar: AppBar(
                title: Text(
                  "个人",
                  style: TextStyle(
                    color: theme.accentColor,
                  ),
                ),
              ),
              body: Container(
                child: Column(
                  children: [
                    Text(
                      "个人",
                      style: TextStyle(
                        color: theme.accentColor,
                      ),
                    ),
                    FlatButton(
                      onPressed: () {
                        setState(() {});
                        currentTheme.toggleTheme();
                      },
                      child: Text(
                        "多主题",
                        style: TextStyle(
                          color: theme.accentColor,
                        ),
                      ),
                    ),
                    Center(
                      child: RaisedButton(
                        onPressed: () async {
                          var result = await channel.invokeMethod(invokeMethod);
                          setState(() {
                            _result = result;
                          });
        
                          print(result);
                        },
                        child: new Text(_result),
                      ),
                    )
                  ],
                ),
              ),
            );
          }
        }
        
          
         
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 31
        • 32
        • 33
        • 34
        • 35
        • 36
        • 37
        • 38
        • 39
        • 40
        • 41
        • 42
        • 43
        • 44
        • 45
        • 46
        • 47
        • 48
        • 49
        • 50
        • 51
        • 52
        • 53
        • 54
        • 55
        • 56
        • 57
        • 58
        • 59
        • 60
        • 61
        • 62
        • 63
        • 64
        • 65
        • 66
        • 67
        • 68
        • 69
        • 70
        • 71
        • 72
        package com.example.flutter_package_demo
        
        //import io.flutter.app.FlutterActivity
        import android.os.Bundle
        import io.flutter.app.FlutterActivity
        import io.flutter.plugin.common.MethodChannel
        import io.flutter.plugins.GeneratedPluginRegistrant
        
        //import io.flutter.embedding.android.FlutterActivity
        
        //import io.flutter.view.FlutterMain
        
        class MainActivity : FlutterActivity() {
        
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
        
        //        GeneratedPluginRegistrant.registerWith(this) // here is the error: Type mismatch. Required: FlutterEngine! Found: MainActivity
        
                MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
                    if (call.method == InvokeMethod) {
                        val greetings = successNativeCode()
                        result.success(greetings)
                    }
                }
            }
        
            private fun successNativeCode(): String {
                return "我是android原生跑过来的数据"
            }
        
            companion object {
                private const val CHANNEL = "www.luckly.work"
                private const val InvokeMethod = "breeze"
            }
        }
          
         
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 31
        • 32
        • 33
        • 34
        • 35

        文章来源: jianguo.blog.csdn.net,作者:坚果前端の博客,版权归原作者所有,如需转载,请联系作者。

        原文链接:jianguo.blog.csdn.net/article/details/119887580

        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
        • 点赞
        • 收藏
        • 关注作者

        评论(0

        0/1000
        抱歉,系统识别当前为高风险访问,暂不支持该操作

        全部回复

        上滑加载中

        设置昵称

        在此一键设置昵称,即可参与社区互动!

        *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

        *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。