Home > OS >  Issue regarding flutter-slider (errors_patch.dart -> AssertionError_throwNew error)
Issue regarding flutter-slider (errors_patch.dart -> AssertionError_throwNew error)

Time:12-31

I'm fairly new to Flutter, and I was trying to implement a horizontal-slider with divisions using the external syncfusion_flutter_sliders library.
Here's my attempt at the implementation:

class SliderExample extends StatefulWidget {
  const SliderExample({super.key});
  @override
  State<SliderExample> createState() => _SliderExampleState();
}

class _SliderExampleState extends State<SliderExample> {
  final double _min = 0;
  final double _max = 100;
  double _value = 40.0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SfSlider(
          min: _min,
          max: _max,
          value: _value,
          interval: 20,
          showTicks: true,
          showLabels: true,
          onChanged: (dynamic newValue) {
            setState(() {
              _value = newValue;
            });
          },
        ),
      ),
    );
  }
}

The above stateful widget when called in another stateless route, throws the following error:

ERROR -> errors_patch.dart

@pragma("vm:external-name", "AssertionError_throwNew")
external static _doThrowNew(
    int assertionStart, int assertionEnd, Object? message);

CALL

return Scaffold(
  body:
    Container(
      alignment: Alignment.center,
      decoration: const BoxDecoration(
          gradient: LinearGradient(colors: [Colors.orangeAccent, Colors.deepOrange]),
      ),
      child: ListView(
        children: const <Widget>[
          Text('\n\n\nHow Many People Did You Meet Today?',
              textAlign: TextAlign.justify,
              style: TextStyle(fontSize: 40, color: Colors.black, fontWeight: FontWeight.bold)
          ),
          MaterialApp(
            home: SliderExample(), // <-- HERE
          )
        ]
      ),
  ),
);

Resources to the flutter library followed: https://pub.dev/packages/syncfusion_flutter_sliders ;p.s. Excuse me if it's an elementary fix, in view of the fact that I'm trying to learn by making an app from scratch using the Flutter Docs.

CodePudding user response:

Try changing this part of your code to this

child: ListView(
        children: const <Widget>[
          Text('\n\n\nHow Many People Did You Meet Today?',
              textAlign: TextAlign.justify,
              style: TextStyle(fontSize: 40, color: Colors.black, fontWeight: FontWeight.bold)
          ),
          SliderExample(),
        ]
      ),

You shouldn't call MaterialApp(home: ) there.

And in _SliderExampleState, change the following part to the one below

Widget build(BuildContext context) {
  return SfSlider(
    min: _min,
    max: _max,
    value: _value,
    interval: 20,
    showTicks: true,
    showLabels: true,
    onChanged: (dynamic newValue) {
      setState(() {
        _value = newValue;
      });
    },
  );
}

Remove the wrapping in Scaffold and Center.

  • Related