Home > Back-end >  Flutter: UnimplementedError, I don't understand why I am getting this error
Flutter: UnimplementedError, I don't understand why I am getting this error

Time:04-04

I'm really new learning to code Flutter Apps (Dart) and I'm following a basic course to build a simple user interface step by step. Everything works fine until I need to create a image slider (in a customised appBar) and when I created a "CardImage" class that returns a final card variable (a container for every single image), Android Studio suggest to insert a trow UnimplementedError() in the class, finally I ran my app and it crashed, showing a exception caught in the console log.

I tried to solve the problem after doing some google searchs and I don't understand why the error dissapears when I comment the throw UnimplementedError() in the "CardImage" class and why Android Studio suggest to insert this piece of code?. Is there a better solution? I will appreciate your help.

App error

My class:

import 'package:flutter/material.dart';

class CardImage extends StatelessWidget {
  final String imagePath;

  CardImage(this.imagePath, {Key? key}) : super(key: key) {
    // TODO: implement
    throw UnimplementedError();
  }

  @override
  Widget build(BuildContext context) {
    final card = Container(
      height: 350.0,
      width: 250.0,
      margin: const EdgeInsets.only(
        top: 80.0,
        left: 20.0
      ),
      decoration: BoxDecoration(
        image: DecorationImage(
          fit: BoxFit.cover,
          image: AssetImage(imagePath)
        ),
        borderRadius: const BorderRadius.all(Radius.circular(10.0)),
        shape: BoxShape.rectangle,
        boxShadow: const <BoxShadow>[
          BoxShadow(
            color: Colors.black38,
            // Nivel de degradado
            blurRadius: 15.0,
            // Posición de la sombra (x, y)
            offset: Offset(0.0, 7.0)

          )
        ]
      ),
    );

    return card;
  }
}

The console log:

Launching lib\main.dart on sdk gphone x86 in debug mode...
Running Gradle task 'assembleDebug'...
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Debug service listening on ws://127.0.0.1:63533/KSRdMP8cgi0=/ws
Syncing files to device sdk gphone x86...

======== Exception caught by widgets library =======================================================
The following UnimplementedError was thrown building CardImageList(dirty):
UnimplementedError

The relevant error-causing widget was: 
  CardImageList CardImageList:file:///C:/Users/joh4n4lex4nder/Documents/Platzi/Cursos/Flutter/Avanzado/lib/header_app.dart:13:15
When the exception was thrown, this was the stack: 
#0      new CardImage (package:trip_app/card_image.dart:8:5)
#1      CardImageList.build (package:trip_app/card_image_list.dart:18:11)
#2      StatelessElement.build (package:flutter/src/widgets/framework.dart:4827:28)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735:5)
#6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729:5)
#7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#8      MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36)
#9      MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32)
...     Normal element mounting (7 frames)
#16     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#17     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36)
#18     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32)
...     Normal element mounting (19 frames)
#37     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#38     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36)
#39     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32)
...     Normal element mounting (255 frames)
#294    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#295    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36)
#296    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32)
...     Normal element mounting (377 frames)
#673    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#674    Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
#675    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16)
#676    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5)
#677    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1112:18)
#678    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2600:19)
#679    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13)
#680    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7)
#681    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:924:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
====================================================================================================
I/OpenGLRenderer( 1507): Davey! duration=725ms; Flags=0, IntendedVsync=159868971593464, Vsync=159868971593464, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=159868980901800, AnimationStart=159868980931200, PerformTraversalsStart=159868980972100, DrawStart=159868982275600, SyncQueued=159869022993700, SyncStart=159869028916400, IssueDrawCommandsStart=159869072195400, SwapBuffers=159869684501800, FrameCompleted=159869703161500, DequeueBufferDuration=7169100, QueueBufferDuration=715000, GpuCompleted=72904454231491230, 

CodePudding user response:

In your code (line 6) I see it explicitly throws the mentioned error, can you please remove that line and try again?

CodePudding user response:

"This Error is thrown by unfinished code that hasn't yet implemented all the features it needs.

If the class does not intend to implement the feature, it should throw an UnsupportedError instead. This error is only intended for use during development."

If you are interested in more go here, https://api.flutter.dev/flutter/dart-core/UnimplementedError-class.html

I believe some people use it as temporary placeholders so that when they reach that part of the code you get an error, reminding them that they still have to complete/implement that part of the code.

Hope this helps

  • Related