Unhandled Exception: RangeError (index): Invalid value: Only valid value is 0: 1

64 views Asked by At

I am new to mobile app development. I am using the flutter framework. in reality I am working on a small application capable of performing ussd requests in the background and displaying the result on the application. I was able to find two pulgins that could allow me to perform this task. but the only problem is that these two plugins have trouble performing multisession ussd requests, i.e. sessions with dialog boxes that present a menu such as (1. confirm the purchase, 2. return to the previous one, etc.) . please I would like someone to help me understand what it is or if someone has already had to work on such a project, that he can help me on a way or a way to circumvent my problem or to realize my project. Thanks a lot.

error of range exceptions

as this image shows, I would like that when I enter a ussd code that opens a multisession, I should be able to manage the sessions via my application and the final result can appear at the bottom of the screen

screens app

E/flutter ( 8544): #0      List.[] (dart:core-patch/growable_array.dart:264:36)
E/flutter ( 8544): #1      new _CodeAndBody.fromUssdCode
package:ussd_advanced/ussd_advanced.dart:93
E/flutter ( 8544): #2      UssdAdvanced.multisessionUssd
package:ussd_advanced/ussd_advanced.dart:33
E/flutter ( 8544): #3      _MultiUssdState.build.<anonymous closure>
package:tussdapp/multi_ussd.dart:136
E/flutter ( 8544): <asynchronous suspension>
E/flutter ( 8544):

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:tussdapp/utils/constants.dart';
import 'package:tussdapp/utils/dimensions.dart';
import 'package:ussd_advanced/ussd_advanced.dart';

class MultiUssd extends StatefulWidget {
  const MultiUssd({super.key});

  @override
  State<MultiUssd> createState() => _MultiUssdState();
}

enum RequestState {
  ongoing,
  success,
  error,
}

class _MultiUssdState extends State<MultiUssd> {
  RequestState? _requestState;
  String _requestCode = "";
  String _responseCode = "";
  String? _responseMessage = "";

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Container(
                width: Dimensions.heightPost,
                height: Dimensions.heightPost,
                decoration: BoxDecoration(
                    //color: Colors.white,
                    image: DecorationImage(
                        image: AssetImage('assets/images/ussd.png'),
                        fit: BoxFit.contain)),
              ),
              Text(
                "Entrez votre code ussd simple",
                style: TextStyle(
                  color: kBlackColor,
                  fontSize: Dimensions.font15,
                  fontWeight: FontWeight.w500,
                ),
              ),
              SizedBox(
                height: Dimensions.defaultPadding,
              ),
              Padding(
                padding: EdgeInsets.only(bottom: Dimensions.height18),
                child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
                      child: TextFormField(
                        onChanged: (value) {
                          setState(() {
                            _requestCode = value;
                          });
                        },
                        //controller: _controller,
                        keyboardType: TextInputType.phone,
                        decoration: InputDecoration(
                            //errorText: validate ? "Value Can't Be Empty" : null,
                            border: OutlineInputBorder(),
                            hintText: " exple: #150#"),
                      ),
                    )),
              ),

              //// bouton  d'envoi du code ussd code
              const SizedBox(height: 15),
              MaterialButton(
                color: Colors.blue,
                textColor: Colors.white,
                onPressed: _requestState == RequestState.ongoing
                    ? null
                    : () async {
                        print(_requestCode);
                        setState(() {
                          _requestState = RequestState.ongoing;
                        });
                        try {
                          String? responseMessage;
                          await Permission.phone.request();
                          if (!await Permission.phone.isGranted) {
                            throw Exception("permission missing");
                          }
                          String? _res = await UssdAdvanced.multisessionUssd(
                              code: _requestCode, subscriptionId: 1);
                          setState(() {
                            responseMessage = _res;
                          });
                          String? _res2 = await UssdAdvanced.sendMessage('0');
                          setState(() {
                            responseMessage = _res2;
                          });
                          await UssdAdvanced.cancelSession();
                          setState(() {
                            _requestState = RequestState.success;
                            _responseMessage = responseMessage;
                          });
                        } on PlatformException catch (e) {
                          setState(() {
                            _requestState = RequestState.error;
                            _responseCode =
                                e is PlatformException ? e.code : "";
                            _responseMessage = e.message ?? '';
                          });
                        }
                        //await sendUssdRequest();
                      },
                child: const Text('Send Ussd'),
              ),
              const SizedBox(height: 15),
              if (_requestState == RequestState.ongoing)
                Row(
                  children: const <Widget>[
                    SizedBox(
                      width: 24,
                      height: 24,
                      child: CircularProgressIndicator(),
                    ),
                    SizedBox(width: 24),
                    Text('Ongoing request...'),
                  ],
                ),
              if (_requestState == RequestState.success) ...[
                const Text('Last request was successful.'),
                const SizedBox(height: 10),
                const Text('Response was:'),
                Text(
                  _responseMessage!,
                  style: const TextStyle(fontWeight: FontWeight.bold),
                ),
              ],
              if (_requestState == RequestState.error) ...[
                const Text('Last request was not successful'),
                const SizedBox(height: 10),
                const Text('Error code was:'),
                Text(
                  _responseCode,
                  style: const TextStyle(fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 10),
                const Text('Error message was:'),
                Text(
                  _responseMessage!,
                  style: const TextStyle(fontWeight: FontWeight.bold),
                ),
              ]
            ],
          ),
        ),
      ),
    ));
  }
}


0

There are 0 answers