小编Sco*_*nts的帖子

如何使用 Flutter MethodChannel 从本机 swift 代码调用 dart 代码中的方法?

我看过很多关于这个主题的类似问题,但没有一个解决方案对我有用。我正在 Flutter 中开发一个应用程序,但想从本机 iOS 项目中调用我的main.dart文件中的特定方法。AppDelegate.swift

为了删除所有其他变量,我已将问题提取到一个新的 dart 项目中。我试图setChannelText()AppDelegate.swiftusing 中调用methodChannel.invokeMethod(),但没有成功。

有人知道我哪里出错了吗?我知道我没有对 中的“名称”参数进行操作methodChannel.invokeMethod(),但那是因为我只想调用该方法......

这是我的 main.dart 文件:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  MethodChannel channel =
      new MethodChannel("com.example.channeltest/changetext");
  String centerText;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.purple,
        body: Center(
          child: Text(
            centerText,
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.bold,
              fontSize: …
Run Code Online (Sandbox Code Playgroud)

ios dart swift flutter

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

dart ×1

flutter ×1

ios ×1

swift ×1