13

I am learning Flutter allthought i dont know if it is right decition or not. Any way i want to use LinearProgressIndicator Component from Material Librery but i didnt get how to use it i tried this code:

import 'dart:async';
import 'package:flutter/material.dart';

void main() {
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new MyApp(),
 ));
}

class MyApp extends StatefulWidget {
@override
MyAppState createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return new Scaffold(
  appBar: new AppBar(
    title: new Text('Slider Demo'),
  ),
  body: new Container(
    color: Colors.blueAccent,
    padding: new EdgeInsets.all(32.0),
    child: new ProgressIndicatorDemo(),
  ),
);
}
}

class ProgressIndicatorDemo extends StatefulWidget {

@override
_ProgressIndicatorDemoState createState() =>
  new _ProgressIndicatorDemoState();
}

class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo>
with SingleTickerProviderStateMixin {
AnimationController _controller;

@override
void initState() {
super.initState();
_controller = new AnimationController(
  duration: const Duration(milliseconds: 1500),
  vsync: this,
);
_playAnimation();
}

 Future<Null> _playAnimation() async {
  try {
  await _controller.repeat().orCancel;
  } on TickerCanceled {
  // the animation got canceled, probably because we were disposed
  }
  }

  @override
  void dispose() {
  _controller.stop();
  super.dispose();
  }

  @override
  Widget build(BuildContext context) {
  return _buildAnimation(context, null);
 }

 Widget _buildAnimation(BuildContext context, Widget child) {
 return new Center(
    child: new Container(
  color: Colors.redAccent.withOpacity(.1),
  child: new LinearProgressIndicator(
    value: _controller.value,
    backgroundColor: Colors.redAccent,
  ),
   ));
  }
 }

but i get always this error

I/flutter ( 3819): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY 
╞═══════════════════════════════════════════════════════════
I/flutter ( 3819): The following NoSuchMethodError was thrown building 
ProgressIndicatorDemo(dirty, state:
I/flutter ( 3819): _ProgressIndicatorDemoState#80400(ticker active)):
I/flutter ( 3819): The getter 'value' was called on null.
I/flutter ( 3819): Receiver: null
I/flutter ( 3819): Tried calling: value
I/flutter ( 3819):
I/flutter ( 3819): When the exception was thrown, this was the stack:
I/flutter ( 3819): #0      Object.noSuchMethod 
(dart:core/runtime/libobject_patch.dart:46:5)
I/flutter ( 3819): #1      _ProgressIndicatorDemoState.build 
(file:///E:/Flutter_work/animation_test/lib/main.dart:67:26)
I/flutter ( 3819): #2      StatefulElement.build 
(package:flutter/src/widgets/framework.dart:3730:27)
I/flutter ( 3819): #3      ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3642:15)
I/flutter ( 3819): #4      Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #5      StatefulElement.update 
(package:flutter/src/widgets/framework.dart:3799:5)
I/flutter ( 3819): #6      Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #7      SingleChildRenderObjectElement.update 
(package:flutter/src/widgets/framework.dart:4661:14)
I/flutter ( 3819): #8      Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #9      SingleChildRenderObjectElement.update 
(package:flutter/src/widgets/framework.dart:4661:14)
I/flutter ( 3819): #10     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #11     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #12     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #13     StatelessElement.update 
(package:flutter/src/widgets/framework.dart:3702:5)
I/flutter ( 3819): #14     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #15     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #16     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #17     ProxyElement.update 
(package:flutter/src/widgets/framework.dart:3909:5)
I/flutter ( 3819): #18     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #19     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #20     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #21     ProxyElement.update 
(package:flutter/src/widgets/framework.dart:3909:5)
I/flutter ( 3819): #22     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #23     RenderObjectElement.updateChildren 
(package:flutter/src/widgets/framework.dart:4379:32)
I/flutter ( 3819): #24     MultiChildRenderObjectElement.update 
(package:flutter/src/widgets/framework.dart:4769:17)
I/flutter ( 3819): #25     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #26     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #27     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #28     StatefulElement.update 
(package:flutter/src/widgets/framework.dart:3799:5)
I/flutter ( 3819): #29     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #30     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #31     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #32     ProxyElement.update 
(package:flutter/src/widgets/framework.dart:3909:5)
I/flutter ( 3819): #33     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #34     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #35     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #36     StatefulElement.update 
(package:flutter/src/widgets/framework.dart:3799:5)
I/flutter ( 3819): #37     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #38     SingleChildRenderObjectElement.update 
(package:flutter/src/widgets/framework.dart:4661:14)
I/flutter ( 3819): #39     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #40     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #41     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #42     StatelessElement.update 
(package:flutter/src/widgets/framework.dart:3702:5)
I/flutter ( 3819): #43     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)   
I/flutter ( 3819): #44     SingleChildRenderObjectElement.update 
(package:flutter/src/widgets/framework.dart:4661:14)
I/flutter ( 3819): #45     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #46     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #47     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #48     StatefulElement.update 
(package:flutter/src/widgets/framework.dart:3799:5)
I/flutter ( 3819): #49     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #50     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #51     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #52     StatefulElement.update 
(package:flutter/src/widgets/framework.dart:3799:5)
I/flutter ( 3819): #53     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #54     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #55     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #56     ProxyElement.update 
(package:flutter/src/widgets/framework.dart:3909:5)
I/flutter ( 3819): #57     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #58     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #59     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #60     ProxyElement.update 
(package:flutter/src/widgets/framework.dart:3909:5)
I/flutter ( 3819): #61     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #62     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #63     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)
I/flutter ( 3819): #64     StatefulElement.update 
(package:flutter/src/widgets/framework.dart:3799:5)
I/flutter ( 3819): #65     Element.updateChild 
(package:flutter/src/widgets/framework.dart:2699:15)
I/flutter ( 3819): #66     ComponentElement.performRebuild 
(package:flutter/src/widgets/framework.dart:3653:16)
I/flutter ( 3819): #67     Element.rebuild 
(package:flutter/src/widgets/framework.dart:3495:5)  
I/flutter ( 3819): #68     BuildOwner.buildScope 
(package:flutter/src/widgets/framework.dart:2242:33)
 Reloaded 0 of 391 libraries in 829ms.
 Lost connection to device.

How to slove that and i treid to do this Demo Flutter Progress Indicator Demo but Also the same Result Any one can help me in working example about that or can tell me what is the wrong in my code ?? Regards

Error Screen Shot

BIS Tech
  • 17,000
  • 12
  • 99
  • 148
DoctorDoom
  • 501
  • 2
  • 8
  • 21
  • I am little bit confused because nothing worked from the native code but it works very good when i used this plugin [flutter_percent](https://github.com/diegoveloper/flutter_percent_indicator) – DoctorDoom Aug 23 '18 at 03:57
  • 1
    most likely it has something to do with `Flutter Hot Reload` (Ctrl + \\) - i had the same issue yesterday with `Slider` - when i was changing `value` property, nothing seemed to work until i used `Flutter Hot Restart` (Ctrl + Shift + \\) - simply try your original code again – pskink Aug 23 '18 at 05:21
  • I tried it but it diddnt Work it works finally onley when i upgrade flutter to master channel – DoctorDoom Aug 24 '18 at 16:16

4 Answers4

30

You can use AlwaysStoppedAnimation simply for valueColor,

LinearProgressIndicator(
    backgroundColor: Colors.red,
    valueColor: AlwaysStoppedAnimation<Color>(Colors.amber,),
    value: 0.8,
),
22

You are not using animation object.

import 'dart:async';
import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    debugShowCheckedModeBanner: false,
    home: new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Slider Demo'),
      ),
      body: new Container(
        color: Colors.blueAccent,
        padding: new EdgeInsets.all(32.0),
        child: new ProgressIndicatorDemo(),
      ),
    );
  }
}

class ProgressIndicatorDemo extends StatefulWidget {

  @override
  _ProgressIndicatorDemoState createState() =>
      new _ProgressIndicatorDemoState();
}

class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    animation = Tween(begin: 0.0, end: 1.0).animate(controller)
      ..addListener(() {
        setState(() {
          // the state that has changed here is the animation object’s value
        });
      });
    controller.repeat();
  }


  @override
  void dispose() {
    controller.stop();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
        child: new Container(
          child:  LinearProgressIndicator( value:  animation.value,),

        )
    );
  }

}
Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61
  • I am little bit confused because this solution didnt worked Thanks alot – DoctorDoom Aug 23 '18 at 03:59
  • It’s work fine for me. Can you add screenshot or errors. – Viren V Varasadiya Aug 23 '18 at 11:23
  • I have update the post please Take alook becouse i copied Your Code and i have changed onley the title and it didnt work – DoctorDoom Aug 23 '18 at 16:13
  • [This Result produced by this Code](https://www.writeurl.com/text/tytyiou7hdxysefaqmi1/vxlerhso99prhyc5ushb/hustucazkiayadx2hjrf) – DoctorDoom Aug 23 '18 at 16:59
  • [Error Log](https://www.writeurl.com/text/icobfu5ws1jaqdsq11b4/42jrh14omma6f11xq8j3) – DoctorDoom Aug 23 '18 at 17:03
  • i used your code and it is completely worked for me. my be this is the problem of version. you should upgrade flutter SDK. this problem also discuss on git hub. https://github.com/flutter/flutter/issues/16096 – Viren V Varasadiya Aug 23 '18 at 17:19
  • Thank you very much i switch my flutter channel to master channel and every things works supper Thank you very much – DoctorDoom Aug 24 '18 at 16:17
  • Would be much better to see an example where one would wait on a Future to be completed or something similar, this is confusion for when to start/stop the progress indicator. – CularBytes Mar 08 '20 at 12:29
5

Try this :

Instead of

value: _controller.value,

Use

value: _controller.value ?? 0.0,

You can also use the package that I created , it has animation:

https://pub.dartlang.org/packages/percent_indicator

diegoveloper
  • 93,875
  • 20
  • 236
  • 194
  • I am little bit confused because nothing worked from the native code but it works very good when i used your plugin i would if icould use the LinearProgressIndicator from flutter but all the solutions faild – DoctorDoom Aug 23 '18 at 03:58
0

The Solution for this Problem was Switching the Flutter Channel from beta to master channel and then the code above worked

DoctorDoom
  • 501
  • 2
  • 8
  • 21