未处理的异常:NoSuchMethodError:“String”类没有实例方法“subString”

时间:2021-04-21 18:45:21

标签: flutter

我无法找到此问题的解决方案,我的错误是字符串没有包含子字符串的实例。 我想打印变量“现在”,但它显示此错误。我尝试了很多与此问题匹配的解决方案,但没有帮助我。请帮帮我 错误: E/flutter(6091):[错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:NoSuchMethodError:类“String”没有实例方法“SubString”。 E/颤振(6091):接收器:“+01:00” E/flutter(6091):尝试调用:subString(1, 3) E/flutter (6091): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) E/flutter (6091): #1 _LoadingState.getTime (package:world_time/pages/loading.dart:22:40) E/颤振(6091):

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'dart:convert';
import 'dart:core';

class Loading extends StatefulWidget {
  @override
  _LoadingState createState() => _LoadingState();
}

class _LoadingState extends State<Loading> {


  void getTime() async {
    //make the request
    Response response = await get(Uri.parse('https://worldtimeapi.org/api/timezone/Europe/London'));
    Map data =jsonDecode(response.body);
    //print(data);

    //get property from data
    String datetime =data['datetime'];
    String offset = data['utc_offset'].subString(1,3);
    //print(datetime);
    //print(offset);

    //create a datetime object
    DateTime now =DateTime.parse(datetime);
    now =now.add(Duration(hours: int.parse(offset)));
    print(now);




  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Text('loading screen'),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

尝试将其更改为:

String offset = data['utc_offset'].toString().substring(1,3);