Ошибка необработанного исключения [177] Переполнение стека

#flutter #dart #stack #overflow #unhandled

#флаттер #dart #стек #переполнение #необработанный

Вопрос:

Итак, я новичок в flutter и пробую это видео, которое я видел. (На самом деле я почти закончил) и на видео инструктор пытался перехватывать утверждения в качестве примеров, когда возникала ошибка. Это работает на нем, но не на мне. итак, я удалил свой try and catch, потому что тогда он работал так, но я не могу вернуть то же самое утверждение, которое у меня было тогда, и застрял на ошибке, которая у меня была, когда я пытался поймать и сегодня. это мой код для класса world_time

 import 'package:http/http.dart';
import 'dart:convert';

class WorldTime {
  String location;
  String time;
  String flag;
  String url;

  WorldTime({this.location, this.flag, this.url});

  Future<void> getTime() async {
    Response response = await get('http://worldtimeapi.org/api/timezone/$url');
    Map data = jsonDecode(response.body);
    //print(data);

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

    DateTime now = DateTime.parse(datetime);
    now = now.add(Duration(hours: int.parse(offset)));

    time = now.toString();
  }

  WorldTime instance =
      WorldTime(location: 'Berlin', flag: 'germany.png', url: 'Europe/Berlin');
}
  

и это мой код в загрузочном приложении

 import 'package:flutter/material.dart';
import 'package:worldtime_app/service/world_time.dart';

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

class _LoadingState extends State<Loading> {
  String time = 'loading';
  void setupworldtime() async {
    WorldTime instance = WorldTime(
        location: 'Berlin', flag: 'germany.png', url: 'Europe/Berlin');
    await instance.getTime();
    print(instance.time);
    setState(() {
      time = instance.time;
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: EdgeInsets.all(50.0),
        child: Text(time),
      ),
    );
  }
}
  

И мой код для домашнего класса:

 import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            FlatButton.icon(
              onPressed: () {
                Navigator.pushNamed(context, '/Choose_Location');
              },
              icon: Icon(Icons.edit_location),
              label: Text("Choose Location"),
            ),
          ],
        ),
      ),
    );
  }
}
  

And my code on Choose_Location class:

 import 'package:flutter/material.dart';

class Choose_Location extends StatefulWidget {
  @override
  _Choose_LocationState createState() => _Choose_LocationState();
}

class _Choose_LocationState extends State<Choose_Location> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      appBar: AppBar(
        backgroundColor: Colors.blue[900],
        title: Text('Choose a Location'),
        centerTitle: true,
        elevation: 0,
      ),
      body: RaisedButton(
        onPressed: () {
          setState(() {});
        },
      ),
    );
  }
}
  

И мой основной класс:

 import 'package:flutter/material.dart';
import 'package:worldtime_app/pages/Home.dart';
import 'package:worldtime_app/pages/Loading.dart';
import 'package:worldtime_app/pages/Choose_Location.dart';

void main() => runApp(MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (context) => Loading(),
        '/Home': (context) => Home(),
        '/Choose_Location': (context) => Choose_Location(),
      },
    ));
  

Я использую http: ^ 0.12.2 в зависимостях pubspec.yaml.

цель приложения — создать приложение мирового времени на flutter, используя Dart в качестве языка.

это ошибка, которую я получаю.

 Performing hot restart...
Syncing files to device CPH1803...
Restarted application in 36,026ms.
E/flutter (29658): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Stack Overflow
E/flutter (29658): #0      new WorldTime (package:worldtime_app/service/world_time.dart:10:3)
E/flutter (29658): #1      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #2      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #3      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #4      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #5      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #6      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #7      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #8      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #9      new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #10     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #11     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #12     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #13     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #14     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #15     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #16     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #17     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #18     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #19     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #20     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #21     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #22     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #23     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #24     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #25     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #26     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #27     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #28     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #29     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #30     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #31     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #32     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #33     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #34     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #35     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #36     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #37     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #38     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #39     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #40     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #41     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #42     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): #43     new WorldTime (package:worldtime_app/service/world_time.dart:33:24)
E/flutter (29658): ...
E/flutter (29658): ...
E/flutter (29658): #6738   Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter (29658): #6739   ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
E/flutter (29658): #6740   Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
E/flutter (29658): #6741   ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5)
E/flutter (29658): #6742   ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
E/flutter (29658): #6743   Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
E/flutter (29658): #6744   Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
E/flutter (29658): #6745   ComponentElement.performRebuild 
  

Я надеюсь, что вы сможете мне помочь, и любые вопросы по моим вопросам, пожалуйста, задайте его, чтобы удовлетворить вас и ответить на мою проблему спасибо.