Flutter Dio response null

huangapple go评论66阅读模式
英文:

Flutter Dio response null

问题

I'm sorry, but it seems like you're asking for code translation, which I can't provide directly. However, I can provide some guidance or explanations if you have specific questions about the code or need help with a particular part of it. Please feel free to ask any specific questions you have about the code, and I'll do my best to assist you.

英文:

Am using flutter Dio library for api calling and on registration page, after the api call for user creation the user is created on database but the response shows null.

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import '../../../services/dio_web_util.dart';
import '../models/first_page_reg_request.dart';

 

class DioClient {
  Future<Response?> userRegistration(
      {required RegisterInfoReq requestData}) async {
    Response? userData;
    try {
      Response userData =
          await WebUtil.createDio().post('/Candidate/RegisterCandidate', data: requestData);
      if (kDebugMode) {
        print('User Info: ${userData}');
      }
    } on DioError catch (e) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx and is also not 304.
      if (e.response != null) {
        if (kDebugMode) {
          print('Dio error!');
        }
        if (kDebugMode) {
          print('STATUS: ${e.response?.statusCode}');
        }
        if (kDebugMode) {
          print('DATA: ${e.response?.data}');
        }
        if (kDebugMode) {
          print('HEADERS: ${e.response?.headers}');
        }
      } else {
        // Error due to setting up or sending the request
        if (kDebugMode) {
          print('Error sending request!');
        }
        if (kDebugMode) {
          print(e.message);
        }
      }
    }
    return userData;
  }
}

Above code is used for the service.Here the userData is prints correct value.
But the response is stored in another page and the function call for that page is

Response? userData= await apiClient.userRegistration(requestData: requestBody);
        if (kDebugMode) {
          print('User Info: ${userData}');
        }

 

here userData is null... 

Anybody have an idea about this??

答案1

得分: 1

以下是翻译好的部分:

你声明并返回以下变量

Response? userData;

但初始化并打印一个新变量

Response userData =

初始化上述声明的变量,否则将从未来函数中返回null。

英文:

You are declaring and returning following variable

Response? userData;

but initializing and printing a new variable

Response userData =

Initialize above declared variable otherwise null will be returned from future function.

huangapple
  • 本文由 发表于 2023年4月4日 18:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定