• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Flutter connection issues to ASP.Net API

Hi all, I have been trying to get a few things working on a project I am look at currently, but as a beginner this isn't working out too well.

I have a database set up on a sqlexpress server, a single database with a single table with 4 fields.

ID: primary key, autoincrementing, INT
JobRef: nvarchar(10)
ScanDate: datetime
Dept: nvarchar(20)

Now, the api I have is running, I can get the values, I can post into the table. but when I try to connect the flutter app I keep getting an error:

Now for the life in me I cannot see where the issue is but if you could aim me in the right direction I would be most grateful.

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'

My research says there's an issue with something been seen as a string and not an int, or the other way round but I don't know enough about the code below to figure it out and i'm following tutorials no end to get this working...

FIXED THIS BIT: I have also noticed that when using chrome to browse, the output seems to come in the form of xml even though I would reaaaally like this in json so other things can be done with it. Again, my lack of knowledge and experience means I'm not sure where to look to find the answer...

Code:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

//  This one works perfectly fine outputting to the console ------
//  String _apiUrl =
//    'https://newsapi.org/v2/everything?q=bitcoin&from=2019-07-13&sortBy=publishedAt&apiKey=44c492d13e1e4727987774a82beea8de';

//  This one doesn't -- website hidden for privacy
String _apiUrl = 'http://hidmywebpage:port/api/BarcodeScans';

List _provider1;

void main() async {
  _provider1 = await fetchData(_apiUrl);

  // outputs to the console log
  for (int i = 0; i < _provider1.length; i++) {
    print(_provider1['ID']); // --> 'title' or 'description'
  }

  runApp(new MaterialApp(
    home: new Categories(),
  ));
}

class Categories extends StatelessWidget {
  [USER=1021285]@override[/USER]
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("News"),
        centerTitle: true,
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            new RaisedButton(
                padding: const EdgeInsets.all(12.0),
                color: Colors.red,
                onPressed: null,
                child: new Text("Get Headlines",
                    style: new TextStyle(
                      fontSize: 21.0,
                      color: Colors.white,
                    )))
          ],
        ),
      ),
    );
  }
}

Future<List> fetchData(String apiUrl) async {
  http.Response response = await http.get(apiUrl);
  return (json.decode(response.body))['ID'];
}

error log:

Launching lib\main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Running Gradle task 'assembleDebug'...
Built build\app\outputs\apk\debug\app-debug.apk.
Installing build\app\outputs\apk\app.apk...
Syncing files to device Android SDK built for x86...
E/flutter (17345): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = ***, port = 41535
E/flutter (17345): #0 IOClient.send (package:http/src/io_client.dart:33:23)
E/flutter (17345): <asynchronous suspension>
E/flutter (17345): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
E/flutter (17345): <asynchronous suspension>
E/flutter (17345): #2 BaseClient.get (package:http/src/base_client.dart:32:7)
E/flutter (17345): #3 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter (17345): #4 _withClient (package:http/http.dart:166:20)
E/flutter (17345): <asynchronous suspension>
E/flutter (17345): #5 get (package:http/http.dart:46:5)
E/flutter (17345): #6 fetchData (package:datavisually_jobscan/main.dart:57:34)
E/flutter (17345): <asynchronous suspension>
E/flutter (17345): #7 main (package:datavisually_jobscan/main.dart:16:22)
E/flutter (17345): <asynchronous suspension>
E/flutter (17345): #8 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:199:25)
E/flutter (17345): #9 _rootRun (dart:async/zone.dart:1124:13)
E/flutter (17345): #10 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (17345): #11 _runZoned (dart:async/zone.dart:1516:10)
E/flutter (17345): #12 runZoned (dart:async/zone.dart:1500:12)
E/flutter (17345): #13 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:190:5)
E/flutter (17345): #14 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:300:19)
E/flutter (17345): #15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
E/flutter (17345):
D/EGL_emulation(17345): eglMakeCurrent: 0xb6185540: ver 3 1 (tinfo 0xb61832d0)
D/EGL_emulation(17345): eglMakeCurrent: 0xb6185420: ver 3 1 (tinfo 0x9597c310)
Lost connection to device.
 
Last edited:
Please use [code][/code] tags to format your code.
And include the stack trace from the Logcat view.

Thanks.
 
Back
Top Bottom