【问题标题】:How to create a screen on socket exception in flutter?如何在颤振中创建套接字异常屏幕?
【发布时间】:2021-09-15 16:03:23
【问题描述】:

在我的颤振项目中,当调用 API 时发生套接字异常时,我需要显示一些插图图像。我该怎么做?

提前致谢

【问题讨论】:

    标签: flutter api dart exception socketexception


    【解决方案1】:

    这取决于您想在小部件树中的哪个位置显示它。一个简单的示例是将新屏幕推送到导航堆栈。您将需要在可能发生异常的函数中使用 BuildContext。

    void someMethod(BuildContext context) {
        try {
          //some code that might throw an exception
        } on Exception catch (_) {
          Navigator.pushNamed(context, "Your illustration view");
        }
      }
    

    另一个示例是根据布尔值将其添加到小部件树中。当抛出异常时,您将该 bool 设置为 true。

    void someOtherMethod() {
        try {
          //some code that might throw an exception
        } on Exception catch (_) {
          setState(() {
          hasThrownError = true; 
          });
        }
      }
    

    像这样在你的小部件树中使用它:

    bool hasThrownError = false;
    
      Widget buildWidgetTree() {
        return hasThrownError
            ? Text("This is where you can show your error illustration")
            : Text("This is wher you can show your regular view");
      }
    

    【讨论】:

      【解决方案2】:

      这将有助于解决套接字异常和格式异常。

      为 httpresponse 创建模型类

      class HTTPResponse<T> {
        bool isSuccessful;
        T data;
        String message;
        int responseCode;
        HTTPResponse(this.isSuccessful, this.data, {this.message, this.responseCode});
      }
      

      然后像这样在api响应中使用这个模型

      Future<HTTPResponse<List<Post>>> getPosts(
            {int limit = 20, int page = 1}) async {
          String url =
              'https://jsonplaceholder.typicode.com/posts?_limit=$limit&_page=$page';
          Uri uri = Uri.parse(url);
      
          try {
            var response = await http.get(uri);
            if (response.statusCode == 200) {
              var body = json.decode(response.body);
              List<Post> postsList = [];
              body.forEach((e) {
                Post post = Post.fromJson(e);
                postsList.add(post);
              });
              return HTTPResponse(
                true,
                postsList,
                responseCode: response.statusCode,
              );
            } else {
              return HTTPResponse(false, null,
                  message: 'Invalid response from server',
                  responseCode: response.statusCode);
            }
          } on SocketException {
            return HTTPResponse(false, [], message: 'Unable to reach the internet');
          } on FormatException {
            return HTTPResponse(false, [], message: 'Invalid response from server');
          } catch (e) {
            return HTTPResponse(false, [],
                message: "Something went wrong please try in a minute or two");
          }
        }
      

      【讨论】:

        猜你喜欢
        • 2020-01-17
        • 2019-08-09
        • 2020-04-15
        • 2021-12-11
        • 1970-01-01
        • 2020-10-10
        • 2021-07-12
        • 2022-12-12
        • 2020-04-18
        相关资源
        最近更新 更多