Error unhandled exeption [177] Stack Overflow

54 Views Asked by At

So I am new to flutter and trying this video I saw. (I am almost finished actually) and at the video, the instructor used to try and catch statements as examples when an error occurred. It does work on him but not on me. so I deleted my try and catch because it used to work like that then but I can't get back the same statement I had then and got stuck on the error I had when I used to try and catch and today. this is my code on world_time class

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');
}

and this is my code in the loading app

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),
      ),
    );
  }
}

And my code on home class:

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(() {});
        },
      ),
    );
  }
}

And my main class:

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(),
      },
    ));

I use http: ^0.12.2 in pubspec.yaml dependencies.

the aim of the app is to make a world time application in flutter using Dart as a language.

this is the error I get.

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 

I hope you can help me and any questions on my questions please ask it to accommodate you and answer my problem thank you.

0

There are 0 best solutions below