我看过很多关于这个主题的类似问题,但没有一个解决方案对我有用。我正在 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)