2d-inc / Nima-Flutter

MIT License
199 stars 37 forks source link

Error when loading assets #9

Closed nartawak closed 5 years ago

nartawak commented 5 years ago

You can find my Flutter sample project here and the Nima sample here

I follow the steps define in the documentation

import "dart:typed_data";

import 'package:flutter/material.dart';
import "package:flutter/services.dart" show rootBundle;
import 'package:nima/nima_actor.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // FIXME: To be removed, this is for testing the .nma asset is loaded
    rootBundle.load("assets/testBZ.nma").then((ByteData data) {
      debugPrint('DATA LOAD $data.runtimeType');
    }).catchError((error) {
      debugPrint('LOAD DATA ERROR, $error');
    });

    return Column(
      children: <Widget>[
        NimaActor("assets/testBZ",
            alignment: Alignment.center,
            fit: BoxFit.contain,
            animation: "idle"),
        Image.asset(
          'assets/testBZ.png',
        ),
      ],
    );
  }
}

As you can see, i add an Image widget for testing that the png asset in accessible in context, and i read the nma file too, it works.

Stacktrace

Launching lib/main.dart on iPhone X in debug mode...
Starting Xcode build...
Xcode build done.                                            6.2s
flutter: DATA LOAD Instance of '_ByteDataView'.runtimeType
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
Unable to load asset: assets/testBZ
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
<asynchronous suspension>
#1      FlutterActor.loadFromBundle (package:nima/nima.dart:190:38)
<asynchronous suspension>
#2      NimaActorRenderObject.filename= (package:nima/nima_actor.dart:197:11)
#3      NimaActor.createRenderObject (package:nima/nima_actor.dart:44:9)
#4      RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4482:28)
#5      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
#6      MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4965:32)
#7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
#8      Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
#9      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
#10     Element.rebuild (package:flut<…>
flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performResize():
flutter: NimaActorRenderObject object was given an infinite size during layout.
flutter: This probably means that it is a render object that tries to be as big as possible, but it was put
flutter: inside another render object that allows its children to pick their own size.
flutter: The nearest ancestor providing an unbounded height constraint is:
flutter:   RenderFlex#c9289 NEEDS-LAYOUT NEEDS-PAINT
flutter:   creator: Column ← MyApp ← [root]
flutter:   parentData: <none>
flutter:   constraints: BoxConstraints(w=375.0, h=812.0)
flutter:   size: MISSING
flutter:   direction: vertical
flutter:   mainAxisAlignment: start
flutter:   mainAxisSize: max
flutter:   crossAxisAlignment: center
flutter:   verticalDirection: down
flutter: The constraints that applied to the NimaActorRenderObject were:
flutter:   BoxConstraints(0.0<=w<=375.0, 0.0<=h<=Infinity)
flutter: The exact size it was given was:
flutter:   Size(375.0, Infinity)
flutter: See https://flutter.io/layout/ for more information.
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      RenderBox.debugAssertDoesMeetConstraints.<anonymous closure> (package:flutter/src/rendering/box.dart:1749:9)
flutter: #1      RenderBox.debugAssertDoesMeetConstraints (package:flutter/src/rendering/box.dart:1823:6)
flutter: #2      RenderBox.size=.<anonymous closure> (package:flutter/src/rendering/box.dart:1543:17)
flutter: #3      RenderBox.size= (package:flutter/src/rendering/box.dart:1543:65)
flutter: #4      NimaActorRenderObject.performResize (package:nima/nima_actor.dart:228:5)
flutter: #5      RenderObject.layout (package:flutter/src/rendering/object.dart:1619:9)
flutter: #6      RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:738:15)
flutter: #7      RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
flutter: #8      RenderView.performLayout (package:flutter/src/rendering/view.dart:147:13)
flutter: #9      RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1509:7)
flutter: #10     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:768:18)
flutter: #11     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:281:19)
flutter: #12     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:677:13)
flutter: #13     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
flutter: #14     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
flutter: #15     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
flutter: #16     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:751:7)
flutter: #18     _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
flutter: #19     _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
flutter: #20     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
flutter: (elided one frame from package dart:async)
flutter:
flutter: The following RenderObject was being processed when the exception was fired:
flutter:   NimaActorRenderObject#57fda NEEDS-LAYOUT NEEDS-PAINT
flutter:   creator: NimaActor ← Column ← MyApp ← [root]
flutter:   parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
flutter:   constraints: BoxConstraints(0.0<=w<=375.0, 0.0<=h<=Infinity)
flutter:   size: Size(375.0, Infinity)
flutter: This RenderObject has no descendants.
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: NimaActorRenderObject object was given an infinite size during layout.
Syncing files to device iPhone X...
flutter: Another exception was thrown: A RenderFlex overflowed by Infinity pixels on the bottom.
flutter: Another exception was thrown: NimaActorRenderObject object was given an infinite size during layout.
umberto-sonnino commented 5 years ago

Hey nartawak, thanks for the report!

I've run your file locally and it seems to work -- you're right though, its extension needs to be .nima for it to work.

I see a few problems with your code though, and it's mostly about the Flutter layout you're trying to use.

FIrst of all, in Flutter it's easier to use a Scaffold to wrap your main layout so that it has a basic layout structure. Secondly, your Column isn't being bound by anything, so it's trying to take up Infinity pixels in height, which generates an error. Lastly, you can't use an "idle" animation, because your character doesn't have one with that name.

You can try something like this to have some visual feedback:

Scaffold(
    body: Column(
        children:
        [
            Container(
            width:100, 
            height: 100, 
            child: NimaActor("assets/testBZ",
                    alignment: Alignment.center,
                    fit: BoxFit.contain,
                    animation: "Arm"
                )
            ),
            Image.asset('assets/testBZ.png',
                    width: 100,
                    height: 100
            )
        ]
    )
);
nartawak commented 5 years ago

Hi @umberto-sonnino, thanks for your answer. I try to implements your solution and rename the file testBZ.nma to testBZ.nima. The same error is throwing.

You can find the sample here

umberto-sonnino commented 5 years ago

I tried the build() method from your PR and it seems to be working correctly on my end. Is the error you're getting the same from your first post?

nartawak commented 5 years ago

@umberto-sonnino Sorry for the late answer, I didn't have much time on this subject. I update flutter on my maschine and the issue is solved ..... So i close this issue, thanks for your help ;)

SudduK commented 4 years ago

Just thought to comment that I had same issue and was fixed after renaming to Nima file name from .nma to .nima and updating the asset name as given below) and performed flutter clean before re-building. _

new NimaActor("assets/NN.nima", alignment:Alignment.center, fit:BoxFit.fill, animation: "NA"),

_