0

When I make a call to this function , it works if the artist and album has data without spaces. How would I change this to make it consider data with spaces .

  static Future fetchAlbumDetailData(String albumName, String artist) async {
    print('ALBUM NAME : $albumName');
    print('ALBUM artist : $artist');
    print(
        'Link called is :https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=$apiKey&artist=$artist&album=$albumName&format=json');
    http.Response response = await retry(
      // Make a GET request
      () => http
          .get(
            Uri.parse(
                'https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=$apiKey&artist=$artist&album=$albumName&format=json'),
             )
          .timeout(const Duration(seconds: 5)),
      // Retry on SocketException or TimeoutException
      retryIf: (e) => e is SocketException || e is TimeoutException,
    );

    if (response.statusCode == 200) {
      return response.body;
    } else {
      throw (response.statusCode);
    }
  }

My print statement output is :

I/flutter ( 5341): ALBUM NAME : Make Believe I/flutter ( 5341): ALBUM artist : Weezer I/flutter ( 5341): Link called is :https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=755bf5e882b716dac814852b5e8e2e52&artist=Weezer&album=Make Believe&format=json

The link is destroyed between the words Make and Believe

3 Answers3

1

The Uri class provides encodeFull, encodeComponent, and encodeQueryComponent methods to escape characters that are reserved for URIs for other purposes.

In your case, since your string will be part of a query string, you should use Uri.encodeQueryComponent. Note that this will encode spaces as + and not as %20, but + is more correct for query string components. (Also see: URL encoding the space character: + or %20?)

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
0

Please replace space with %20 before making get call. like this. albumName.replaceAll(" ","%20")

Mofidul Islam
  • 378
  • 3
  • 12
  • Thanks. The url now is combined . While I click on the link now which is printed , it opens fine . But when calling http.get , it returms 'Null check operator used on a null value' . Can I not use Uri.parse ? WIll I have to use Uri.encode or something ? – Anish Hemachandran Oct 04 '21 at 17:00
  • You should use one of the `Uri.encode...` functions instead of manually replacing characters. – jamesdlin Oct 04 '21 at 18:43
0

You don't actually need to replace the String as the Uri.parse turns your space into %20 for you.

Here I test this simple app and seems the call is working fine.

As for your null check operator error, it usually happens with the exclamation notation! when you use on a value to force it to be not-null, but it actually is null. It would help if you share more of your code (null-safe related parts) so we can support further.

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final uri =
      'https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=755bf5e882b716dac814852b5e8e2e52&artist=Weezer&album=Make Believe&format=json';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Sample App'),
        ),
        body: Container(
            child: Center(
          child: TextButton(
            child: Text('Press me'),
            onPressed: () => fetchAlbumDetailData(uri),
          ),
        )),
      ),
    );
  }

  static Future fetchAlbumDetailData(_uri) async {
    print(Uri.parse(_uri));
    // Print out: https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=755bf5e882b716dac814852b5e8e2e52&artist=Weezer&album=Make%20Believe&format=json
    final result = await http
        .get(Uri.parse(_uri))
        .timeout(const Duration(seconds: 5));

    print(result.body);
  }
}
Bach
  • 2,928
  • 1
  • 6
  • 16
  • Thanks Bach . You were right. I found out later that the data models in the api were different for some of the albums . Thus the null error was occurring for those albums which didn't have the data . My bad . Thanks for clearing it up for me – Anish Hemachandran Oct 04 '21 at 23:47