简体   繁体   English

如何传递参数以将数据发布到REST API?

[英]How do I pass through parameters in order to post data to a REST API?

I am trying to post multiple data items to a REST API in JSON format. 我正在尝试将多个数据项以JSON格式发布到REST API。 I am able to post the username and password however am unable to pass through the other details pertaining to the device such as manufacturer, model and version. 我能够发布用户名和密码,但是无法传递与设备有关的其他详细信息,例如制造商,型号和版本。 I have created a function to pass through the additional parameters however in the function itself the parameters are not being pass through to the private variables I am assigning them to. 我创建了一个函数来传递其他参数,但是在函数本身中,参数并未传递给我为其分配给它们的私有变量。 Here is my code currently: 这是我目前的代码:

login screen 登入画面

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

import 'home_screen.dart';
import '../auth/auth.dart';
import '../data/database_helper.dart';
import '../models/user.dart';
import '../screens/login_screen_presenter.dart';

import 'dart:ui';
import 'dart:io';

class LoginScreen extends StatefulWidget{

  @override
  State<StatefulWidget> createState(){
    return new LoginScreenState();
  }
}

class LoginScreenState extends State<LoginScreen>
  implements LoginScreenContract, AuthStateListener{
    BuildContext _ctx;

    bool _isLoading = false;
    final formKey = new GlobalKey<FormState>();
    final scaffoldKey = new GlobalKey<ScaffoldState>();

    String _password;
    String _username;
    String _manufacturer;
    String _model;
    String _version;

    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    AndroidDeviceInfo androidDeviceInfo;
    IosDeviceInfo iosDeviceInfo;

    LoginScreenPresenter _presenter;

    LoginScreenState(){
      _presenter = new LoginScreenPresenter(this);
      var authStateProvider = new AuthStateProvider();
      authStateProvider.subscribe(this);
      initPlatform();
    }

    Future<Null> initPlatform() async {
      AndroidDeviceInfo aInfo;
      IosDeviceInfo iInfo;


      try{
        if (Platform.isAndroid) {
          aInfo = await deviceInfoPlugin.androidInfo;
        } else if (Platform.isIOS){
          iInfo = await deviceInfoPlugin.iosInfo;        
        } 
      }
      catch (e){
        print(e.toString());
      }

      if(!mounted) return;

      setState(() {
            androidDeviceInfo = aInfo;
            iosDeviceInfo = iInfo;
            function(_manufacturer,_model,_version);
          });
    }

     function(_manufacturer,_model,_version){
         _manufacturer = androidDeviceInfo.manufacturer;
         _model =  androidDeviceInfo.model;
         _version =  androidDeviceInfo.version;   
    }  

    void _submit(){
      final form = formKey.currentState;

      if (form.validate()) {
        setState(() => _isLoading = true);
        form.save();
        function(_manufacturer,_model,_version);
        _presenter.doLogin(_username, _password, _manufacturer, _model, _version);
      } 
    }

    void _showSnackBar(String text) {
      scaffoldKey.currentState
        .showSnackBar(new SnackBar(content: new Text(text)));
    }

    @override
    onAuthStateChanged(AuthState state){
      if (state == AuthState.LOGGED_IN)
        Navigator.of(_ctx).pushReplacementNamed('/home');
    }

    @override
    Widget build(context){
      _ctx = context;
      return Scaffold(
        key: scaffoldKey,
        appBar: new AppBarDesign(),
        drawer: new Drawer(
          child: new ListView(
            children: [
              new ListTile(
                title: new Text('Home'),
                trailing: new Icon(Icons.arrow_right),
                onTap: (){Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => HomeScreen()),
            ); },
              )
            ],
          ),
        ),        
        body: Form(
          key: formKey,
          child: ListView(
            children: [
              //  Banner 
              new Column(
                children: [
                  new Row( 
                    children: [ 
                      new Expanded(
                        child:Image(
                        image: new AssetImage('assets/Banner.png'), 
                        fit: BoxFit.fitWidth              
                        )
                      )            
                    ]
                  ),      
                ],
              ),

              new  Container(padding: EdgeInsets.only(top:10.0)),

              //Instructional text
              new Container(
                child: new Column(
                  children:[
                    Text('Please enter your username'),
                    Text('and password below to start'),
                    Text('using the application'),
                  ]
                ),
              ),

              new  Container(padding: EdgeInsets.only(top:4.0)),

              // Links to username widget, password widget and loginbtn widget
              new Column( 
                children: [ 
                  usernameField(context),
                  passwordField(context),
                  new  Container(padding: EdgeInsets.only(top:10.0)),           
                  _isLoading ? new CircularProgressIndicator():  submitButton(context)                  
                ],
              ),
            ]
          ) 
        ),          
      );
    }  

    Widget deviceInfo(BuildContext context){
      return FutureBuilder(
        future: initPlatform(),
        builder: (BuildContext context, AsyncSnapshot snapshot){
          if (snapshot.connectionState == ConnectionState.none){
            return Text('Error: ${snapshot.error}');
          } else {
            return Column(
              children: [
                function(_manufacturer,_model,_version),                
              ]
            );
          }
        }
      );
    } 

    Widget usernameField(context){       
      return TextFormField(
        onSaved: (val) => _username = val,      
        keyboardType: TextInputType.emailAddress,
        validator: (val){
          return val.length < 10
              ? "Username must have atleast 10 characters"
              : null;
        },
        decoration: InputDecoration(
          hintText: '',
          labelText: 'Username',
        ) 
      );      
    }

    Widget passwordField(context){        
      return TextFormField(
        onSaved: (val) => _password = val,
        validator: (val){
          return val.length < 4
              ? "Password must have atleast 5 characters"
              : null;
        },
        obscureText: true,
        decoration: InputDecoration(
          hintText: 'Password',
        labelText: 'Password',
        ),
      );
    }

    Widget submitButton(context){
      return RaisedButton(
        child: Text('Login'),
        color: Colors.grey[150],
        onPressed: _submit,
      );
    } 


  @override
  void onLoginError(String errorTxt) {
    _showSnackBar(errorTxt);
    setState(() => _isLoading = false);
  }

  @override
  void onLoginSuccess(User user) async {
    _showSnackBar(user.toString());
    setState(() => _isLoading = false);
    var db = new DatabaseHelper();
    await db.saveUser(user);
    var authStateProvider = new AuthStateProvider();
    authStateProvider.notify(AuthState.LOGGED_IN);
  }
}

class AppBarDesign extends AppBar{

  AppBarDesign():super(
    title: Text('FlashLight'),    
          backgroundColor: Color.fromRGBO(32,32,32,32),
          actions: [
            Icon(
              IconData(0xe885, fontFamily: 'MaterialIcons'),
            ),
          ],
  );
}

login screen presenter 登录屏幕演示者

import 'package:login_bloc/src/data/rest_ds.dart';
import 'package:login_bloc/src/models/user.dart';

abstract class LoginScreenContract {
  void onLoginSuccess(User user);
  void onLoginError(String errorTxt);
}

class LoginScreenPresenter {
  LoginScreenContract _view;
  RestDatasource api = new RestDatasource();
  LoginScreenPresenter(this._view);

  doLogin(String username, String password, String manufacturer, String model, String version) {
    api.login(username, password, manufacturer, model, version).then((dynamic res) {

      print(res.toString());
      print(res["LoginMessage"]);
      if(!res["LoginSuccessful"]) {
        _view.onLoginError(res["LoginMessage"]);
        return;
      }

      _view.onLoginSuccess(User.map(res["user"]));
    }).catchError((Exception error) { 
      _view.onLoginError(error.toString());
    });
  }
}

rest_ds rest_ds

import 'dart:async';

import 'package:login_bloc/src/utils/network_util.dart';
import 'package:login_bloc/src/models/user.dart';

class RestDatasource {
  NetworkUtil _netUtil = new NetworkUtil();
  static final BASE_URL = "API url";
  static final LOGIN_URL = BASE_URL + "API url detail";
  static final _API_KEY = "somerandomkey";

  Future<dynamic> login(String username, String password, String manufacturer, String model, String version ) {

    return _netUtil.post(LOGIN_URL, body: {
      "token": _API_KEY,
      "username": username,
      "password": password,
      "manufacturer": manufacturer,
      "model": model,
      "version": version, 
    });
  }
}

add to the loginscreen an initState() and call the initPlatform() 在登录屏幕上添加一个initState()并调用initPlatform()

initState will run when you initialize the class LoginScreenState 初始化类LoginScreenState时,initState将运行

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

找到的解决方案:似乎由于我将类变量创建为私有变量,因此未将它们正确分配给函数内的androidDeviceInfo。[property]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过eXist-db的REST api传递xquery? - How do I pass xquery through eXist-db's REST api? 如何通过 cURL 正确地向 REST API 发送 POST 请求? - How do I send POST request to REST API properly through cURL? 如何在REST POST snaplogic中传递主体参数? - How to pass body parameters in REST POST snaplogic? Spring DSL的URI如何传递REST获取参数 - URI to Spring DSL How do I pass in REST Get Parameters 我正在尝试通过 azure 数据工厂将数据从 REST API 下载到 azure 数据湖。 如何在 azure 数据工厂中传递 API 的查询参数? - I am trying to download data from REST API to azure data lake via azure data factory. How can I pass query parameters for API in azure data factory? 在REST API中传递参数的最佳选择是什么-POST类型的方法? - what is best option to pass parameters in REST api - POST type of method? 如何获得 POST 请求以在 Confluence REST API 中工作? - How do I get a POST request to work in Confluence REST API? 如何在REST API的URL中传递列表? - How do I pass a list in url for a REST api? 如何将值从 REST API 传递到 Leaflet Z46F3EA056CAA31268CZZEA0 - How do I pass value from REST API to Leaflet Map 如何通过Magento REST API获取数据 - How can I get data through the Magento REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM