首页 > 解决方案 > 当我尝试在 Flutter/dart 中获取函数的值时出现问题

问题描述

嗨,我无法获得返回我的函数 postTest() 的 de 值

                           String usuario = usernameController.text;
                                  print('Si $usuario');
                                  String password = passwordController.text;
                                  print('Password: $password');

                                  print("${postTest(usuario, password)}");
                                }
                              },
                            ),
                            Registrate(),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 500, left: 170),
                child: ImagenAmbulancia(),
              )
            ],
          ),
        ),
      ),
    );
  }

  postTest(usuario, password) async {
    final uri = 'http://10.0.2.2:5000/base/';

    http.Response response = await http
        .post(Uri.parse(uri), body: {"User": usuario, "Pass": password});
    var repuesta = response.body;
    String estadoUsuario = jsonDecode(repuesta)["respuesta"];
    return estadoUsuario;
  }
}

我尝试保存让我 postTest 但我不能...返回我 Future 的值,但我想要字符串

标签: flutterdartflutter-layoutdart-polymer

解决方案


当你想使用你的方法时,因为它的未来类型(并且将在未来填写)你应该.then在你这样调用你的方法时使用

postTest(usuario, password).then((value) {
 print(value);
});

推荐阅读