Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将按钮渲染到main.dart文件?另外,是否有任何方法可以在不使用任何按钮的on pressed()的情况下将参数传递给命名路由?_Java_Http_Flutter_Async Await_Flutter Plugin - Fatal编程技术网

Java 如何将按钮渲染到main.dart文件?另外,是否有任何方法可以在不使用任何按钮的on pressed()的情况下将参数传递给命名路由?

Java 如何将按钮渲染到main.dart文件?另外,是否有任何方法可以在不使用任何按钮的on pressed()的情况下将参数传递给命名路由?,java,http,flutter,async-await,flutter-plugin,Java,Http,Flutter,Async Await,Flutter Plugin,这是main.dart,当前参数是在按下按钮()时传递的,我请求建议一种方法(如果有的话)将参数从main.dart(例如)传递到plugin.dart,而不使用按钮 void main() { runApp(new MaterialApp( home: new MyApp(), routes: { ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(), },

这是main.dart,当前参数是在按下按钮()时传递的,我请求建议一种方法(如果有的话)将参数从main.dart(例如)传递到plugin.dart,而不使用按钮

void main() {
  runApp(new MaterialApp(
    home: new MyApp(),
    routes: {
      ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(),
    },
  ));
}

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('My test app'),
      ),
      body: Center(
        child: Text('This example app'),
      ),

      // A button that navigates to a named route. The named route extracts the arguments by itself.
      floatingActionButton: FloatingActionButton(
        child: Text("Boutton"), //button that is passing arguments to named route.
        onPressed: () {
          
          Navigator.pushNamed(
            context,
            ExtractArgumentsScreen.routeName,
            arguments: ScreenArguments(
              'Click me',                       //custom button name
              'abc',                            //api key
            ),
          );
        },
      )
    );
  }
}

void main(){
runApp(新材料)PP(
主页:新建MyApp(),
路线:{
ExtractArgumentsScreen.routeName:(上下文)=>ExtractArgumentsScreen(),
},
));
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
字符串_platformVersion='未知';
@凌驾
void initState(){
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:const Text(“我的测试应用程序”),
),
正文:中(
child:Text(“此示例应用程序”),
),
//导航到命名路由的按钮。命名路由本身提取参数。
浮动操作按钮:浮动操作按钮(
child:Text(“Boutton”),//将参数传递给命名路由的按钮。
已按下:(){
Navigator.pushNamed(
上下文
ExtractArgumentsScreen.routeName,
参数:屏幕参数(
'单击我',//自定义按钮名称
'abc',//api密钥
),
);
},
)
);
}
}
下面是plugin.dart文件,我在其中发出http get请求。 我如何在main.dart中调用它,以便按钮以相同的方式渲染

class ScreenArguments {
  final String buttonName;

  final String api;

  ScreenArguments(this.buttonName, this.api);
}

class ExtractArgumentsScreen extends StatefulWidget {
  _ExtractArgumentsScreen createState() => _ExtractArgumentsScreen();
  static const routeName = '/extractArguments';
}

// A widget that extracts the necessary arguments from the ModalRoute.
class _ExtractArgumentsScreen extends State<ExtractArgumentsScreen> {
  Future<String> myButton() async {
    // Extract the arguments from the current ModalRoute settings and cast
    // them as ScreenArguments.

    final ScreenArguments args = ModalRoute.of(context).settings.arguments;
    String url = "example.com/project/details?key=" + args.api;
    final response = await http.get(url);
    // headers: {"Accept":"application/json"});

    if (response.statusCode == 200) {
      print(response.body);
      return args.buttonName;
    }
    return null;
  }

  Widget build(BuildContext context) {
    return FutureBuilder<String>(
        future: myButton(),
        builder: (context, AsyncSnapshot<String> snapshot) {
          if (snapshot.hasData) {
            return FloatingActionButton(        //this button has to be rendered to main.dart file
              child: Text(snapshot.data),
              onPressed: () {
                MyForm();
              },
            );
          } else {
            return CircularProgressIndicator();
          }
        }
        );
  }
}

class MyForm extends State<ExtractArgumentsScreen> {
  @override
  Widget build(BuildContext context) {
    // code for form widget
  }
}
类参数{
最终字符串按钮名;
最终字符串api;
屏幕参数(this.buttonName、this.api);
}
类ExtractArgumentScreen扩展StatefulWidget{
_ExtractArgumentsScreen createState()=>\u ExtractArgumentsScreen();
静态常量routeName='/extractArguments';
}
//从ModalRoute中提取必要参数的小部件。
类_ExtractArgumentsScreen扩展状态{
Future myButton()异步{
//从当前ModalRoute设置和强制转换中提取参数
//把它们当作论据。
final ScreenArguments args=ModalRoute.of(context.settings.arguments);
String url=“example.com/project/details?key=“+args.api;
最终响应=等待http.get(url);
//标题:{“接受”:“应用程序/json”});
如果(response.statusCode==200){
打印(响应.正文);
返回args.buttonName;
}
返回null;
}
小部件构建(构建上下文){
回归未来建设者(
future:myButton(),
生成器:(上下文,异步快照){
if(snapshot.hasData){
return FloatingActionButton(//此按钮必须呈现到main.dart文件中
子项:文本(snapshot.data),
已按下:(){
MyForm();
},
);
}否则{
返回循环ProgressIndicator();
}
}
);
}
}
类MyForm扩展了状态{
@凌驾
小部件构建(构建上下文){
//表单小部件的代码
}
}
这种方法在语法上正确吗